Перезагрузите карусель после AJAX звоните Vue. js - PullRequest
0 голосов
/ 19 февраля 2020

На моей странице есть компонент карусели, в который данные загружаются через AJAX вызов сервера. с этой частью все в порядке, но когда элементы карусели присоединяются к карусели, она разрушается. Так что мне нужно перерисовать карусель после AJAX успеха. У кого-нибудь есть идеи? Я много искал, но ничего не нашел для vue. js.

Мой код:

<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;
        })
    }

}

1 Ответ

1 голос
/ 19 февраля 2020

Просто добавьте атрибут key к вашему компоненту карусели и затем меняйте его значение после каждого вызова AJAX - это сделает Vue для повторного рендеринга компонента:

<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++;
        });
  }
...