小程序页面之间传递对象的数据
主页
// 主页
let list = {
id:1,
name: beibei,
sex: '男',
age: 21,
src: 'https://blog.csdn.net/Kavii_a'
}
// 假如list是我们从后台获取过来的数据,我们需要传递给别的页面
// 需要将数据转换成json字符串,不然传不过去
list = JSON.stringify(list);
wx.navigateTo({
// encodeURIComponent把字符串作为 URI 组件进行编码
// 我的应用场景是在后台拿到的数据是富文本数据,其中包含图片数据
// 不使用encodeURIComponent函数就会报错,只有编码解码才能穿传递过去
url: `./index/index?list=${encodeURIComponent(list)}`,
})
详情页
onLoad: function(options) {
// decodeURIComponent()函数对 encodeURIComponent()函数编码的 URI 进行解码
let list = decodeURIComponent((options.list));
// 将json字符串转换成js对象
list = JSON.parse(list)
}