Vue怎么实现分批加载数据

发布时间:2022-04-11 15:36:41 作者:iii
来源:亿速云 阅读:316

本篇内容主要讲解“Vue怎么实现分批加载数据”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue怎么实现分批加载数据”吧!

分批加载数据

最近在写vue的项目,因为后台返回的数据量太大,在调用了高德地图渲染"polygon"覆盖物的时候处理不过来,直接蹦掉了,然后后台小哥哥和我讲可以分批处理~没想到真的是快了很多很多,眼过千变不如手过一遍~,在此记录一下!!!

首先我们需要定义四个全局的变量

export default {
  name: "map_app",
  inject:['reload'],
  data() {
    return {
      pagindex: 1, //页码
      pagesize: 300, //页/条数
      pagetotal: 0, //一共要请求的次数
      intertimer: null, //定时器
     }
   }
}

然后再methods中写定时器 让定时器每隔三秒再去执行一个方法;

//定时器
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); //每三秒调用一次方法
      }, 3000);
 },

然后再这个方法里面我们去做判断,如果当前请求的页数超过一共要请求的次数就清楚定时器!

//定时器2
intervalData(map) {
   if (this.pagindex > this.pagetotal) {
        clearInterval(this.intertimer); //关闭定时器
        this.loading.close(); //关闭弹窗
        this.pagindex = 1;
    } else {
        this.renderMesh(map); //数据渲染
        this.pagindex += 1;
      }
},

总数是后台小哥哥返回的,然后我们每次去请求接口的时候要给后台传当前是第几页,还有要请求多少条数据

renderMesh(map) { 
     this.$axios
       .get(this.httpApi + "/api/Main/GetBlockMap", {
          params: {
            BlockCode: this.pageid,
            page: this.pagindex, //当前页码
            rownum: this.pagesize //请求数量
          }
      })
      .then(res => {
       console.log(res);
      })
      .catch(err => {
       console.log("请求失败233");
       });
}

因为我的总数是调用的另外一个接口,然后也写一下代码

    this.$axios
    .get(this.httpApi + "/api/Main/GetBlockMapCount", {
          params: {
            BlockCode: this.pageid
          }
     })
     .then(res => {
          let jsonData = eval("(" + res.data + ")");
          //总数除每次请求多少条数据得出一共要请求多少次
          this.pagetotal = Math.ceil(jsonData.totals / this.pagesize); 
      })
      .catch(err => {
          console.log("请求失败");
      });

滚动加载数据

核心方法:

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);
    },

监听:

 mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll, false);
  },

到此,相信大家对“Vue怎么实现分批加载数据”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Tensorflow如何实现分批量读取数据
  2. vue 使用鼠标滚动加载数据的例子

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue

上一篇:JavaScript中ceil的概念是什么

下一篇:vue怎么实现web滚动条分页

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》