javascript - 为什么 v-for & axios 不一个个加载项目?
问题描述
我正在使用 vue-2.6.11、axios-0.21.1、vuetify-2.4.3 创建产品网络应用程序
我从本地数组中获取类别,然后使用 v-for 将 fetchUrl 作为 Props 传递到 Row 组件中。然后在 Row 组件中,我在获得 API 响应后使用 axios 获取 fetchUrl,我只是安装它。它工作正常,但问题是类别对象意味着 Row 组件以随机顺序加载导致 Row 组件安装,因为它从 API 获得 axios 响应。
所以我希望 Next row await 直到上部完全安装或其他任何东西使它有序加载。
我的组件:
主页.vue -
<template>
<div>
<div v-for="(categories,index) in categories" :key="`${index}`">
<ItemsCarousel
:title="categories.title"
:fetch-url="categories.fetchUrl"
/>
</div>
</div>
</template>
<script>
import categoriesList from '@/local-database/Categories.json';
import ItemsCarousel from '@/components/carousel/ItemsCarousel';
export default {
name: 'Home',
components: {
ItemsCarousel
},
data: () => ({
categories: categoriesList.filter( categories => (catalogue.for==true || categories.for=="home"))
})
}
</script>
ItemsCarousel.vue -
<template>
<div class="items-carousel">
<v-lazy v-model="isActive" :options="{threshold: 0.5}">
<h1>{{title}}</h1>
<div class="items-carousel" v-for="product in products" :key="product.id">
<Card v-bind="{...product}">/>
</div>
</v-lazy>
</div>
</template>
<script>
import ProductManger from '@/mixins/ProductManger';
import Card from '@/components/Card';
export default {
name: 'ItemsCarousel',
mixins: [ProductManger], // Axios Setup
components: {
Card
},
props: ['title','params'],
data: () => ({
isActive: false,
cards: []
}),
methods: {
async loadCard() {
this.contentMangerCore(this.params) // Function code inside mixins
.then(res => {
this.cards = res.data;
})
}
},
mounted() {
this.loadCard();
}
};
</script>
数据样本:-
类别列表.json-
[{
"id": 1,
"name": "Adventure",
"params": {
"categories": "Adventure",
"sort": "ASC"
}
}, {
"id": 2,
"name": "Art",
"params": {
"categories": "Art",
"sort": "DESC"
}
}, {
"id": 3,
"name": "Beauty",
"params": {
"categories": "Art",
"sort": "DESC"
}
}, {
"id": 4,
"name": "Business",
"params": {
"categories": "Art",
"sort": "DESC"
}
}, {
"id": 5,
"name": "Craft",
"params": {
"categories": "Art",
"sort": "DESC"
}
},...]
products.json-
[{
"name": "AdventureIRC",
"img": "..."
},
{
"name": "Adventie",
"img": "..."
},...]
我希望你们能帮助我解决这个问题......谢谢:微笑:
解决方案
您可以创建一个计算方法来确定在任何给定时间实际显示的类别数量,并随着成功的 axios 请求而增加。
get categoriesForDisplay() {
return this.categories.slice(0, this.successfulCarouselFetches + 1)
}
然后定义successfulCarouselFetches
:
data() {
return {
//
successfulCarouselFetches : 0
}
}
listen
对于组件中成功的 axios 请求Item-Carousel
:
<ItemsCarousel
:title="categories.title"
:fetch-url="categories.fetchUrl"
@success="successfulCarouselFetches = successfulCarouselFetches + 1"
/>
success
现在,每当您的 xhr 完成工作时广播:
methods: {
async loadCard() {
this.contentMangerCore(this.params) // Function code inside mixins
.then(res => {
this.cards = res.data;
this.$emit('success');
})
}
},
当页面加载时,页面上有一个Item-Carousel
组件,它将执行第一个 XHR 请求。当组件$emit
的事件时,包含 的父组件v-for
将增加 ,这将successfulCarouselFetches
允许 gettercategoriesForDisplay
在.Item-Carousel
v-for
我相信,这基本上可以满足您的要求。
推荐阅读
- sql - 仅当其中一列为真时才向多列添加约束
- ffmpeg - 如何通过谷歌字体在ffmpeg中加载字体
- visual-studio-2019 - 当我们提供物联网边缘连接字符串时设置物联网边缘模拟器问题
- powershell - 从 cmd.exe 启动时写入主机不工作
- go - Helm Secret 在 helm secret 问题中连接两个值
- reactjs - React JS 中 Angular JS $transitions.onBefore 事件的替代方法是什么
- vue.js - 在 vue.js 中的 Select 2 中编辑时预选值
- c - CentOS8实时FIFO进程未收到XCPU信号
- javascript - Angular - 如何在不参考 hostView 的情况下删除动态组件
- validation - 在 Go Validator.v2 中处理嵌套结构