为什么我的小程序每次刷新的时候都发送请求,无法使用旧的数据? 求大佬帮忙看看!
发布于 6 年前 作者 limin 14826 次浏览 来自 问答

Page({

  data: {
    leftMenuList:[],
    rightMenuList:[],
    currentIndex:0
  },
  Cates:[]
  onLoad: function (options) {
    this.getCategories();

    const Cates = wx.getStorageSync("cates");
    if(!Cates){
      this.getCategories();
    }else{
      if(Date.now() - Cates.time > 1000*1000){
        this.getCategories();
      }else{
        this.Cates = Cates.data;
        let newLeft = this.Cates.map(v => v.cat_name);
        let newRight = this.Cates[0].children;
        this.setData({
          leftMenuList:newLeft,
          rightMenuList:newRight
        })
      }
    }
  },
  getCategories(){
    request({url: 'https://api-hmugo-web.itheima.net/api/public/v1/categories'})
    .then(result =>{
      this.Cates=result.data.message;

      wx.setStorageSync("cates", {time:Date.now(),data:this.Cates});

      let newLeft = this.Cates.map(v => v.cat_name);
      let newRight = this.Cates[0].children;
      this.setData({
        leftMenuList:newLeft,
        rightMenuList:newRight
      })
    })
  }
})

代码:

调试窗口:

1 回复

没办法加载缓存,每次都向接口发送请求,弄了半个钟都不行,哭了QAQ

回到顶部