VueJS 2.5.3中vue-carousel-3d需点击Chrome开发者工具才渲染的问题求助
I’ve run into similar issues with vue-carousel-3d before—this usually happens because the component doesn’t detect when your async data loads, so it fails to calculate its layout properly. Opening DevTools forces a browser repaint, which kicks off the component’s layout logic. Here’s how to fix it:
Step 1: Fix your Vue instance setup
First, move your data fetch to the mounted hook (instead of beforeCreate) and clean up the this handling. Also, fix the missing quote in your delimiters config:
new Vue({ el: '#ttt', data: { events: [], }, mounted() { // Use arrow functions to keep `this` bound to the Vue instance axios.get('/api/afisha/') .then(response => { this.events = response.data; // Wait for DOM to update, then trigger the carousel to refresh this.$nextTick(() => { // If the carousel has a built-in refresh method, use that first // this.$refs.carousel.refresh(); // If not, force a component update to trigger layout recalculation this.$forceUpdate(); }); }) .catch(err => console.error('Error fetching events:', err)); }, components: { 'carousel-3d': Carousel3d.Carousel3d, 'slide': Carousel3d.Slide }, delimiters: ["[[", "]]"] // Fixed missing closing quote });
Step 2: Update your template
Add a ref to the carousel component for easier access, and add a key to your v-for (this helps Vue track slide elements properly):
<div id="ttt" class="container-fluid screen-2 panel" style="top: calc(1*100%);"> <div class="promo-slid"> <p>袩芯写懈胁褨褌褜褋褟 薪邪 薪邪褕懈褏 泻芯褌褟褌!</p> </div> <div class="slider"> <carousel-3d ref="carousel" :controls-visible="true" :controls-prev-html="'❬'" :controls-next-html="'❭'" :controls-width="30" :controls-height="60" :clickable="false" > <slide v-for="(event, i) in events" :key="event.id || i" :index="i"> <figure> <img :src="event.images" alt="Event visual"> </figure> </slide> </carousel-3d> </div> </div>
Why this works:
mountedhook: This runs after the Vue instance is attached to the DOM, so the carousel component exists when your data loads.beforeCreateruns too early—when the carousel initializes, there’s no data, and it doesn’t re-check later.$nextTick: Ensures we wait until Vue has updated the DOM with the neweventsdata before triggering the carousel refresh.$forceUpdate: Forces the component (and its children, like the carousel) to re-render, which triggers the layout calculations the carousel missed when data was loaded asynchronously.keyinv-for: Helps Vue efficiently update the slide elements instead of reusing them incorrectly, which can cause rendering glitches.
Alternative: Use a watcher
If the carousel has a dedicated refresh method (check its docs), you can use a watcher to trigger it when events changes:
watch: { events(newEvents) { if (newEvents.length) { this.$nextTick(() => { this.$refs.carousel.refresh(); }); } } }
内容的提问来源于stack exchange,提问作者Ihor Dreyev

