最近在寫vue的項目,由於後臺返回的數據量太大,致使頁面卡死直接蹦掉了,而後後臺小哥哥和我講能夠分批處理~沒想到真的是快了不少不少,眼過千變不如手過一遍~,在此記錄一下!!! 首先咱們要定義兩個變量:
vue
data() { page: 0, number: 1 }ios
getData(){
this.$axios.post("/api/XXX", {}).then(res=>{
if (res.data.rows) {
let inquiryRelates = res.data.rows.InquiryRelates;
if (inquiryRelates.length < 5) {
this.InquiryRelates = inquiryRelates;
this.loadData = false;
} else {
this.lazyLoadCount(this.page, this.number, inquiryRelates);
}
}
})
}
lazyLoadCount(page, number, inquiryRelates) {
this.InquiryRelates = this.InquiryRelates.concat(inquiryRelates.slice(page, number));
if (this.InquiryRelates.length < inquiryRelates.length) {
setTimeout(() => {
this.page += 1;
this.number += 1;
this.lazyLoadCount(this.page, this.number, inquiryRelates);
}, 1000);
} else {
this.loadData = false;
}
},
複製代碼
}axios