Preface
You can understand it as: carry a complex object data parameter to realize the jump page reception.
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.
solution
Use JSON
two methods of objects:
- JSON.strtingify: The
object
conversion of a string - JSON.parse: parse the string into
object
.
/*
* [发送参数]
* 将对象解析为字符串
*/
wx.navigateTo({
url: '/pages/index?data=' + JSON.stringify(object),
})
/*
* [接收参数]
* 将字符串解析为对象
*/
this.setData({
data: JSON.parse(options.data)
})
Precautions
You need to pay particular attention to the first , if there is an image link, it must be triggered, resolve to follow the tutorial.
- If there is a string transformed
"?","&"或"="
and other such symbols, only the symbols passed the previous string symbol after the data will be lost, I guess the question might be a small internal routing procedures to deal with these symbols sensitive to it. So sometimes we'll let you useencodeURIComponent()
transcoding, and then in the target pagedecodeURIComponent()
decode, so you can avoid data lost. If you do not understand, please visit the detailed code tutorial! - Do not use parameters
object
to use variables to save up and then passed, so when writing received may not be resolved successfully.