uniapp 切换选项卡仅第一次切换页面请求数据,优化列表性能及用户体验。

页面onload时,初次加载数据


页面初次加载

向下滑动一部分,切换第二栏,再返回第一栏。


滑动列表

切换至第二栏

返回第一栏

1.**u-tabs为uview框架组件
html:

  <u-tabs
      :list="list"
      :current="current"
      @change="onTabchange"
      style="width: 100%"
    ></u-tabs>
    <swiper
      style="width: 100%; height: 100%"
      :current="current"
      circular
      @change="onSwiperChange"
    >
      <swiper-item
        v-for="(item, index) of list"
        :key="index"
        style="overflow-y: auto; height: 300px"
      >
        <view v-for="i in 100" :key="i">{{ i }}</view>
      </swiper-item>
    </swiper>

js:

export default {
  components: {},
  data() {
    return {
      list: [
        {
          name: "待收货",
        },
        {
          name: "待付款",
        },
        {
          name: "待评价",
        },
      ],
      current: 0,
      hasLoad: [],
    };
  },
  onLoad(e) {
    this.getData();
  },
 onPullDownRefresh() {
    this.hasLoad.splice(this.current, 1);
    this.getData();
  },
  mounted() {},
  methods: {
    onTabchange(e) {
      const { index } = e;
      this.current = index;
    },
    onSwiperChange(e) {
      const { current } = e.detail;
      this.current = current;
      this.getData();
    },
    getData() {
      console.log("已经切换过的索引列表:", this.hasLoad);
      console.log(
        "当前索引是否在已请求索引列表里:",
        this.hasLoad.includes(this.current)
      );
      if (this.hasLoad.includes(this.current)) return;
      this.hasLoad.push(this.current);
      setTimeout(() => {
        console.log("模拟请求数据");
      }, 200);
    },

  },
};

TODO:最好是给每个tab对象增加一个data数组,每个swiper-item分别管理自己的数据,再请求完数据之后,塞给当前current的data里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容