最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue如何實現(xiàn)分批加載數(shù)據(jù)

 更新時間:2022年04月11日 14:20:26   作者:周金魚啊  
這篇文章主要介紹了Vue如何實現(xiàn)分批加載數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

分批加載數(shù)據(jù)

最近在寫vue的項目,因為后臺返回的數(shù)據(jù)量太大,在調(diào)用了高德地圖渲染"polygon"覆蓋物的時候處理不過來,直接蹦掉了,然后后臺小哥哥和我講可以分批處理~沒想到真的是快了很多很多,眼過千變不如手過一遍~,在此記錄一下!??!

首先我們需要定義四個全局的變量

  • pagindex 頁碼
  • pagesize 一頁要請求多少條數(shù)據(jù)
  • pagetotal 一共要請求多少次(總數(shù) / pagesize),總是是后臺返回的~
  • intertimer存的定時器的函數(shù),方便清除定時器
export default {
? name: "map_app",
? inject:['reload'],
? data() {
? ? return {
? ? ? pagindex: 1, //頁碼
? ? ? pagesize: 300, //頁/條數(shù)
? ? ? pagetotal: 0, //一共要請求的次數(shù)
? ? ? intertimer: null, //定時器
? ? ?}
? ?}
}

然后再methods中寫定時器 讓定時器每隔三秒再去執(zhí)行一個方法;

//定時器
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);
?},

然后再這個方法里面我們?nèi)プ雠袛?,如果?dāng)前請求的頁數(shù)超過一共要請求的次數(shù)就清楚定時器!

//定時器2
intervalData(map) {
? ?if (this.pagindex > this.pagetotal) {
? ? ? ? clearInterval(this.intertimer); //關(guān)閉定時器
? ? ? ? this.loading.close(); //關(guān)閉彈窗
? ? ? ? this.pagindex = 1;
? ? } else {
? ? ? ? this.renderMesh(map); //數(shù)據(jù)渲染
? ? ? ? this.pagindex += 1;
? ? ? }
},

總數(shù)是后臺小哥哥返回的,然后我們每次去請求接口的時候要給后臺傳當(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");
? ? ? ?});
}

因為我的總數(shù)是調(diào)用的另外一個接口,然后也寫一下代碼

? ? 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("請求失敗");
? ? ? });

滾動加載數(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);
? },

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。   

相關(guān)文章

最新評論

积石山| 太康县| 华亭县| 佳木斯市| 丰顺县| 灵川县| 高邮市| 瑞金市| 商水县| 桐乡市| 城固县| 闸北区| 永德县| 肇州县| 通州区| 焦作市| 凤山市| 古蔺县| 稷山县| 惠安县| 六枝特区| 厦门市| 虞城县| 灵山县| 宜川县| 和平区| 丰台区| 彩票| 交城县| 凤城市| 法库县| 南和县| 建昌县| 探索| 疏附县| 高平市| 建德市| 静安区| 威海市| 枣强县| 凯里市|