为什么我的小程序每次刷新的时候都发送请求,无法使用旧的数据? 求大佬帮忙看看!
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
})
})
}
})
代码:
调试窗口: