Ad

Reload Carousel After AJAX Call In Vue.js

I have a carousel component in my page in which data is loaded via AJAX call to server. this part is OKAY but when the carousel items append to carousel, it wrecks. So I need to re-render the carousel after AJAX success. Does anybody have any idea? I have searched a lot but couldn't find anything for vue.js.

My code:

<carousel class="original-carousel" :dots="false" :nav="false">
                            <router-link class="product-item" to="/product" v-for="product in originalGames">
                                <div class="image-holder">
                                    <img v-bind:src="product.thumbnail_url" v-bind:alt="product.name">
                                    <div class="product-info">
                                        <div class="product-platform">
                                            origin
                                            <img src="/src/assets/imgs/platform/origin-color.svg" >
                                        </div>
                                        <div class="product-region">
                                            US
                                        </div>
                                    </div>
                                </div>
                                <h2 class="product-title">{{product.name}}</h2>
                                <div class="product-bottom">
                                    <div class="product-card-price">
                                        <div class="original-price__holder" >
                                            <span class="sale-range" v-if="product.sale_percent !== false">%{{product.sale_percent}}</span>
                                            <span class="original-price" v-if="product.sale_percent !== false"> {{product.regular_price}}</span>
                                        </div>
                                        <span class="sale-price">{{product.price}}</span>
                                    </div>
                                    <button class="add-to-cart btn btn--circle btn--transparent">+</button>
                                </div>
                            </router-link>
                        </carousel>


export default {
    name: "homePage",
    components: {searchBar, topNav, siteFooter, carousel},
    data(){
        return {
            sliderProducts: [],
            originalGames: [],
            todayHots: [],
        }
    },
    created: function () {
        let th = this;
        axios.get('myAPIUrl').then((response) => {
            th.originalGames = response.data[2].items;
            th.todayHots = response.data[2].items_hot;
        })
    }

}
Ad

Answer

Simply add a key attribute to your carousel component and then change its value after each AJAX call - this will make Vue to re-render the component:

<carousel :key="refresh" ...>
...
</carousel>

<script>
export default
{
  data()
  {
    return {
      refresh: 0,
      ...
    };
  },
  created()
  {
    axios.get('myAPIUrl').then((response) => {
            this.originalGames = response.data[2].items;
            this.todayHots = response.data[2].items_hot;
            this.refresh++;
        });
  }
Ad
source: stackoverflow.com
Ad