使用setStore和getStore缓存页面查询条件和当前页

该博客讨论了在实际应用中,如何处理页面刷新时导致的查询条件和页码丢失问题。提出了一种解决方案,即使用本地存储(sessionStorage)来缓存查询条件和当前页码。通过导入并使用'setStore'和'getStore'方法,保存和获取页面数据,确保在页面刷新后能够恢复之前的状态,提高用户体验。
摘要由CSDN通过智能技术生成

缓存查询条件和当前页

实际应用中,很多时候当我们刷新页面时,本来的查询条件和页码都会置空和置零,这会带来很多不便。例如:当我们需要比较A页面和B页面的某个数据时,会需要在两个页面中来回切换,如果一直刷新页面就非常不方便。
解决方案如下:

  1. 导入setStore和getStore
import { setStore, getStore } from '@/util/store';
  1. 缓存数据
//缓存数据
cachePage() {
      const tmp = getStore({ name: 'all_page_cache' }) || [];
      if (
        tmp &&
        tmp.length > 0 &&
        tmp.find((res) => res.path === this.$route.path) !== undefined
      ) {
        tmp.map((res) => {
          if (res.path === this.$route.path) {
            res.pageSize = this.page.pageSize;
            res.form = this.query;
          }
        });
      } else {
        const arr = {
          path: this.$route.path,
          pageSize: this.page.pageSize,
          form: this.query
        };
        tmp.push(arr);
      }
      setStore({ name: 'all_page_cache', content: tmp, type: 'session' });
    },

//获取页面数据方法
getData(page,query){
  ......
  this.cachePage()
},
  1. 获取缓存的数据
created() {
    // 获取 form 表单查询条件缓存
    const ALL_PAGE_CACHE = getStore({ name: 'all_page_cache' }) || [];
    const _this = this;
    ALL_PAGE_CACHE &&
    ALL_PAGE_CACHE.length > 0 &&
    ALL_PAGE_CACHE.map((res) => {
      if (res.path === this.$route.path) {
        _this.page.pageSize = res.pageSize;
        _this.query = {
          ...res.form
        };
      }
    });
    this.getData(this.page, this.query); //获取到缓存的数据后再调用方法
    this.form = this.deepClone(this.query);   //将获取到的数据赋值给form表单
  }
};
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值