前些天做微信小程序跳转到另一个小程序,同时携带用户的个人信息到另一个小程序,搞了好久传递的参数一直在另一个小程序中接收不到,研究老半天才发现跳进了小程序的坑。因为是个bug,官方进行了修复,要更新最新的开发者工具。废话不多说了,进入正题。
微信小程序能正常跳转到另一个小程序的前提是:这两个小程序被同一个微信公众号关联,否则无法跳转。代码如下:
wx.navigateToMiniProgram({
appId: 'xxxxxxxxxxxxxx',
path: '',
extraData: {
user_id: 111,
store_id: 222,
userName: '张三'
},
envVersion: 'release',
success(res)
{
console.log('跳转成功');
}
})
注意:这里传递了三个参数,分别是user_id,store_id和userName,接下来要在第二个小程序中获取这三个参数。获取代码写在app.js中的onlunch()函数中,启动的时候就获取,(也可以在onshow()函数中获取)代码如下:
data: {
"user_id": '',
"store_id": '',
"userName": ''
},
onLaunch: function(options)
{
console.log(this.data)
console.log(options.referrerInfo.extraData)
//启动时获取参数
this.data.user_id = options.referrerInfo.extraData.user_id;
this.data.store_id = options.referrerInfo.extraData.store_id;
this.data.userName = options.referrerInfo.extraData.userName;
//将用户id,店铺id,用户名放到缓存中
wx.setStorageSync("userinformation", this.data);
var u = wx.getStorageSync("userinformation")
console.log('22222222' + u.store_id);
},
那么,在开发过程中,如何进行调试呢,打开开发者工具,自定义编译条件,然后每次编译当前自定义的编译条件即可。
然后编译时选择“商家中心”如上图模式名称,进行编译就可以模拟进行传参调试。
注意,模拟编译时有个“启动参数”选项,关于这个参数的获取请参考博文:
https://blog.csdn.net/qq_20372833/article/details/80559421