我有以下vue 2模板(简化版):
<template>
<div>
<div v-for="(data, index) in allData" :key="index">
<app-collection :data="data" :index="index"></app-collection>
</div>
</div>
</template>
以下是我的数据:
data: function(){
return {
allData: []
}
}
我有一个"加载更多"按钮,当我点击它时,会调用一个从API获取数据的方法,然后在forEach循环中将它们添加到allData中,代码如下:
this.allNFT.push({name: "name 1", age: 25"})
我的问题是每次添加新数据时,它会重新渲染整个列表而不仅仅是在末尾添加新数据。
有没有办法避免这种情况,只是追加新数据?
以下是我简化后的代码全局概述(我还没有在线API):
<template>
<div>
<div id="collectionList" class="form-group" v-else>
<div class="row">
<div class="col-lg-4" v-for="(data, index) in allData" :key="data.assetId+'_'+index">
<app-collection :data="data" :index="index"></app-collection>
</div>
</div>
<button class="btn btn-primary" v-if="loadMore" @click="getallData()">Load more</button>
<div v-else class="text-center">{{ allData.length ? 'All data loaded' : 'No data found' }}</div>
</div>
</div>
</template>
<script>
import collection from '@/components/content/collection/collection.vue'
export default {
data: function(){
return {
loadMore: true,
allData: [],
perpage: 25,
currentPage: 1
}
},
components: {
'app-collection': collection
},
created: function(){
this.init()
},
methods: {
init: async function(){
await this.getallData()
},
getallData: async function(){
let filtered = {
"page": this.currentPage,
"perpage": this.perpage,
}
try{
let getData = await fetch(
"http://localhost:3200/secondary/paginate-filter",
{
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(
filtered
)
}
)
getData = await getData.json()
if(getData.length){
getData.forEach((elm) => {
this.allData.push({name: elm.name, age: elm.age})
})
}
this.currentPage++
if(getData.length < this.perpage){
this.loadMore = false
}
}catch(e){
console.log(e)
}
},
}
};
</script>