以下为跳转方法:
wx.navigateTo({
url: '../index/index?dataInfo=' + encodeURIComponent(JSON.stringify(dataInfo))
+ '&id=' + id,
success: function (res) { },
fail: function (res) { },
complete: function (res) { },
})
其中dataInfo为对象,对象传递过程中我们需要使用JSON将其转换为JSONStr传递,encodeURIComponent方法是为了将转换后的JSONStr进行编码这样保证了其中的URI正常传递,因为如果对象中由URI,不做编码的话,接收的地方会包JSON转换异常,所以为了防止出现以上问题,建议对象传递都使用encodeURIComponent编码一下;
以下为接收方法:
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let dataInfoBean = JSON.parse(decodeURIComponent(options.dataInfo));
console.log("dataInfoBean :", dataInfoBean )
}
接收时注意需要对JSONStr使用decodeURIComponent进行转码后,在使用JSON转换成对应的对象,这样一个完整的可以包含URI的对象就传递完毕了。
问题原由,小程序项目后台增加了头像字段后,原有的功能界面发生错误无法正常展示,遂定位出代码中有部分对象传递为使用encodeURIComponent进行编码导致。