当前位置: 首页 > 知识库问答 >
问题:

javascript - 请问微信小程序 为什么二次刷新页面 会导致onload 的options 获取不到?

向子安
2023-05-06
onLoad: function onLoad(options) {
        let goods_id = options.goods_id
        let activity_id = options.activity_id
        this.setData({
            goods_id: goods_id,
            activity_id:activity_id
        });
    },
    onShow(){

      var that = this;
      // util.login().then(result => { 
      wx.getStorage({
        key: 'session_key',
        success: function success(res) {
          that.assist_bargain_goods_info_fn();
          that.assist_bargain_log_list_fn();
        }, fail: function fail() {
          _util2.default.login();
        }
      });
    },

_util2.default.login(); 这是一个判断有没有登录,没有登录就会请求登录接口的功能,这个登录接口,调用会刷新页面,但是再次触发会导致 onLoad 里的options为undefined 导致我这些goods_id 和 activity_id都出错 接口无法请求

但是我页面其实参数又都在,不知道怎么去做了 不知道我有没有说清楚

共有2个答案

端木承业
2023-05-06

有点像我早上碰到的情况,但是不知道是不是,试下从开发工具删除掉小程序项目,再打开

朱炳
2023-05-06

1.在 onLoad 函数中,把 goods_id 和 activity_id 保存到本地存储:

onLoad: function onLoad(options) {
    let goods_id = options.goods_id
    let activity_id = options.activity_id
    this.setData({
        goods_id: goods_id,
        activity_id:activity_id
    });
    
    wx.setStorage({
        key: 'goods_id',
        data: goods_id
    });
    wx.setStorage({
        key: 'activity_id',
        data: activity_id
    });
},

2.在 onShow 函数中,从本地存储中获取 goods_id 和 activity_id:

onShow() {
    var that = this;
    wx.getStorage({
        key: 'goods_id',
        success: function (res) {
            that.setData({ goods_id: res.data });
        }
    });
    wx.getStorage({
        key: 'activity_id',
        success: function (res) {
            that.setData({ activity_id: res.data });
        }
    });

    wx.getStorage({
        key: 'session_key',
        success: function success(res) {
            that.assist_bargain_goods_info_fn();
            that.assist_bargain_log_list_fn();
        },
        fail: function fail() {
            _util2.default.login();
        }
    });
}
 类似资料: