微信小程序页面间传值被截断
页面间传递长字符串(URL)和数组对象时
JSON.stringify()和JSON.parse()踩坑!!!
SyntaxError: Unexpected token % in JSON at position 0
SyntaxError: Unexpected end of JSON input
解决办法
//传输数据前 用encodeURIComponent()函数进行编码
//data为数组对象实例数据
const data = {
nickname:'demo',
avatar:'https://***.png'
}
const user = encodeURIComponent(JSON.stringify(data))
wx.navigateTo({
url:`/pages/other/other?user=${user}`
})
// 传输数据后
// 1.用decodeURIComponent()函数解码
// 2.最后再通过JSON.parse()将其还原为数组对象
onLoad(options) {
const user = JSON.parse(decodeURIComponent(options.user))
const { avatar, nickname } = user
this.setData({
avatar,
nickname
})
},