Map 实战

一个页面上面是Tab菜单,每一个Tab菜单下面都有分页,我要把Tab菜单所对应的index。和分页所对应的第几页存起来,这样的话下次进来就直接进入这个Tab所对应的页数。

<v-tabs v-model="currentTabIndex" :tabs="tabs" @change="changeTab"></v-tabs>

<uni-pagination  :current="currentPage" :total="total" :pageSize="pageSize" @change="fnChangePage"/>

要保存到localStorage里边儿的数据是这样的currentPage: [[3,2],[2,1]]
代表第三个Tab菜单看到了第二页,第二个Tab菜单看到了第一页。

实现代码如下。

created(){
  uni.getStorage({
    key: 'currentPage',
    success: res => {
      let map = new Map(JSON.parse(res.data));
      if (map.has(this.currentTabIndex)) {
        this.currentPage = map.get(this.currentTabIndex);
      }
    }
  });
},

fnChangePage({ current } = val){
  this.currentPage = current;
  const currentPage = uni.getStorageSync('currentPage') ? JSON.parse(uni.getStorageSync('currentPage')) : [];
  const map = new Map(currentPage);//数组转Map
  map.set(this.currentTabIndex, current)
  console.log(map);
  uni.setStorage({
    key: 'currentPage',
    data: JSON.stringify([...map]), //Map转为数组
  });
},

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

推荐阅读更多精彩内容