Vue如何實(shí)現(xiàn)分批加載數(shù)據(jù)
分批加載數(shù)據(jù)
最近在寫vue的項(xiàng)目,因?yàn)楹笈_(tái)返回的數(shù)據(jù)量太大,在調(diào)用了高德地圖渲染"polygon"覆蓋物的時(shí)候處理不過來,直接蹦掉了,然后后臺(tái)小哥哥和我講可以分批處理~沒想到真的是快了很多很多,眼過千變不如手過一遍~,在此記錄一下?。。?/p>
首先我們需要定義四個(gè)全局的變量
pagindex
頁碼pagesize
一頁要請求多少條數(shù)據(jù)pagetotal
一共要請求多少次(總數(shù) / pagesize),總是是后臺(tái)返回的~intertimer
存的定時(shí)器的函數(shù),方便清除定時(shí)器
export default { ? name: "map_app", ? inject:['reload'], ? data() { ? ? return { ? ? ? pagindex: 1, //頁碼 ? ? ? pagesize: 300, //頁/條數(shù) ? ? ? pagetotal: 0, //一共要請求的次數(shù) ? ? ? intertimer: null, //定時(shí)器 ? ? ?} ? ?} }
然后再methods中寫定時(shí)器 讓定時(shí)器每隔三秒再去執(zhí)行一個(gè)方法;
//定時(shí)器 getPageInter(map) { ? this.loading = this.$loading({ //加載層 ? ? ? ? lock: true, ? ? ? ? text: "拼命加載中", ? ? ? ? spinner: "el-icon-loading", ? ? ? ? background: "rgba(0, 0, 0, 0.7)" ? ? }); ? ? ? this.intertimer = setInterval(() => { ? ? ? ?this.intervalData(map); //每三秒調(diào)用一次方法 ? ? ? }, 3000); ?},
然后再這個(gè)方法里面我們?nèi)プ雠袛啵绻?dāng)前請求的頁數(shù)超過一共要請求的次數(shù)就清楚定時(shí)器!
//定時(shí)器2 intervalData(map) { ? ?if (this.pagindex > this.pagetotal) { ? ? ? ? clearInterval(this.intertimer); //關(guān)閉定時(shí)器 ? ? ? ? this.loading.close(); //關(guān)閉彈窗 ? ? ? ? this.pagindex = 1; ? ? } else { ? ? ? ? this.renderMesh(map); //數(shù)據(jù)渲染 ? ? ? ? this.pagindex += 1; ? ? ? } },
總數(shù)是后臺(tái)小哥哥返回的,然后我們每次去請求接口的時(shí)候要給后臺(tái)傳當(dāng)前是第幾頁,還有要請求多少條數(shù)據(jù)
renderMesh(map) {? ? ? ?this.$axios ? ? ? ?.get(this.httpApi + "/api/Main/GetBlockMap", { ? ? ? ? ? params: { ? ? ? ? ? ? BlockCode: this.pageid, ? ? ? ? ? ? page: this.pagindex, //當(dāng)前頁碼 ? ? ? ? ? ? rownum: this.pagesize //請求數(shù)量 ? ? ? ? ? } ? ? ? }) ? ? ? .then(res => { ? ? ? ?console.log(res); ? ? ? }) ? ? ? .catch(err => { ? ? ? ?console.log("請求失敗233"); ? ? ? ?}); }
因?yàn)槲业目倲?shù)是調(diào)用的另外一個(gè)接口,然后也寫一下代碼
? ? this.$axios ? ? .get(this.httpApi + "/api/Main/GetBlockMapCount", { ? ? ? ? ? params: { ? ? ? ? ? ? BlockCode: this.pageid ? ? ? ? ? } ? ? ?}) ? ? ?.then(res => { ? ? ? ? ? let jsonData = eval("(" + res.data + ")"); ? ? ? ? ? //總數(shù)除每次請求多少條數(shù)據(jù)得出一共要請求多少次 ? ? ? ? ? this.pagetotal = Math.ceil(jsonData.totals / this.pagesize);? ? ? ? }) ? ? ? .catch(err => { ? ? ? ? ? console.log("請求失敗"); ? ? ? });
滾動(dòng)加載數(shù)據(jù)
核心方法:
handleScroll: function () { var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; var windowHeitht = document.documentElement.clientHeight || document.body.clientHeight; var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; if (scrollTop + windowHeitht >= scrollHeight - 2000) { if (this.scroll) { this.GetSpecialData(); } } }, GetSpecialData() { this.scroll = false; this.page.pageIndex++; this.load(this.page, this.query); },
監(jiān)聽:
?mounted() { ? ? window.addEventListener("scroll", this.handleScroll); ? }, ? destroyed() { ? ? window.removeEventListener("scroll", this.handleScroll, false); ? },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件
最近遇到個(gè)功能要求,想要在全局中調(diào)用組件,而且要在某些js文件內(nèi)調(diào)用,所以這篇文章主要給大家介紹了關(guān)于如何利用Vue3實(shí)現(xiàn)一個(gè)可以用js調(diào)用的組件的相關(guān)資料,需要的朋友可以參考下2021-08-08Vue?vant-ui框架實(shí)現(xiàn)上拉加載下拉刷新功能
功能需求——獲取后端接口返回的數(shù)據(jù),實(shí)現(xiàn)列表數(shù)據(jù)上滑加載更多下一頁數(shù)據(jù),下拉數(shù)據(jù)刷新功能,結(jié)合vant-ui框架實(shí)現(xiàn)??芍苯訁⒖际褂?/div> 2022-09-09Vue的route-view子頁面調(diào)用父頁面的函數(shù)詳解
這篇文章主要介紹了Vue的route-view子頁面調(diào)用父頁面的函數(shù)詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Vue中@change、@input和@blur的區(qū)別及@keyup介紹
這篇文章主要給大家介紹了關(guān)于Vue中@change、@input和@blur的區(qū)別及@keyup介紹的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02Vue實(shí)現(xiàn)雙token無感刷新的示例代碼
這篇文章主要介紹了Vue實(shí)現(xiàn)雙token無感刷新,雙token機(jī)制,尤其是指在OAuth 2.0授權(quán)協(xié)議中廣泛使用的access token(訪問令牌)和refresh token(刷新令牌)組合,文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-03-03Vue實(shí)現(xiàn)手機(jī)號、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí))
這篇文章主要介紹了Vue實(shí)現(xiàn)手機(jī)號、驗(yàn)證碼登錄(60s禁用倒計(jì)時(shí)),幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-12-12最新評論