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

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

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

分批加載數(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)文章

最新評(píng)論

凤山市| 五寨县| 泾阳县| 饶阳县| 利辛县| 德化县| 平阳县| 抚顺县| 庐江县| 平湖市| 涞源县| 天祝| 万年县| 萨迦县| 五华县| 石台县| 瑞昌市| 平凉市| 康马县| 常宁市| 阿尔山市| 济宁市| 额尔古纳市| 城步| 广汉市| 宝丰县| 武清区| 兴义市| 巴东县| 读书| 广东省| 崇文区| 德阳市| 昌平区| 凤凰县| 偏关县| 临沂市| 西和县| 鄱阳县| 大化| 庄河市|