You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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="'&#10092;'" 
      :controls-next-html="'&#10093;'" 
      :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:

  • mounted hook: This runs after the Vue instance is attached to the DOM, so the carousel component exists when your data loads. beforeCreate runs 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 new events data 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.
  • key in v-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:29:16