Preface
In WeChat Mini Programs, wx.navigateTo({})
it is common to use APIs to carry parameters to jump pages, but today there is a requirement to carry a complex data. It is an array with N objects nested in it, and there is an array in the object. So I need to use JSON.stringify
an object into a string, and then pass in the past.
My scenario: where
image
property contains links to articles, has led toJSON.parse
decoding failure, because they were intercepted.
solution
With encodeURIComponent()
transcoding, and then in the target page decodeURIComponent()
decoding.
/*
* [发送参数]
* 1. 将对象解析为字符串
* 2. 把字符串作为 URI 组件进行编码
*/
wx.navigateTo({
url: '/pages/index?data=' + encodeURIComponent(JSON.stringify(object)),
})
/*
* [接收参数]
* 1. 将字符串解析为对象
* 2. 把字符串作为 URI 组件进行解码
*/
this.setData({
data: JSON.parse(decodeURIComponent(options.data))
})