微信小程序开发

小程序转发参数失败(异步)

2018-09-16  本文已影响6人  Sean_Yang

小程序点击open-type="share"按钮中是会自动触发转发当前页面的,同时也可以通过以下函数转发(进行过修改)。

onShareAppMessage: function (ops) {
    console.log(ops.target.id)
    var that = this
    return ({
      title: "邀请您加入" + that.data.parameter,
      path: '/pages/index/index?parameter_name=' + that.data.parameter
    })
},

但是,如果在return外部增加一个request请求,想通过request请求中的新参数来决定转发页面的参数,比如下面的代码,就会因为request需要时间而提前出触发转发当前页面,从而导致这个return是失效的。

onShareAppMessage: function (ops) {
    console.log(ops.target.id)
    var that = this
    wx.request({
        url:"example_url",
        data:{
            xxx:yyy
        },
        success:function(res){
            return ({
                title: "邀请您加入" + res.data.data.parameter,
                path: '/pages/index/index?parameter_name=' + that.data.parameter
            })
        }
    })
},

尝试了很久之后,发现了两种解决途径

  1. 通过分离按钮/界面request的按钮和share的按钮分离,需要按下带request按钮后才能够按share

  2. 通过预请求提前设置初始参数,并通过一定途径避免重复的可能性

上一篇 下一篇

猜你喜欢

热点阅读