解决微信页面跳转传入特殊字符串乱码问题

1.通过路由传入的参数只能是字符串。

常见的用法是用 JSON.stringify() 方法把数组或对象转成字符串,再由url传入

const listData = JSON.stringify(array);
wx.navigateTo({
	url: `/pages/child?listData=${listData}`
})

然后再child页面的onLoad里接收,用 JSON.parse() 方法转换,这样就拿到了想要的数据

onLoad(options) {
    this.list = JSON.parse(options.listData);
 }

2.重点来了:但是有些特殊字符串没办法直接传。(比如html页面)

这样的话就要先加密,再解密解决。
这里用到的就是js原生方法 encodeURIComponent() 将字符串加密,接收页面用 decodeURIComponent() 方法解密类似于下面

const listData = encodeURIComponent(JSON.stringify(array));
wx.navigateTo({
	url: `/pages/child?listData=${listData}`
})

然后再child页面的onLoad里接收

onLoad(options) {
    this.list = JSON.parse(decodeURIComponent(options.listData));
 }

猜你喜欢

转载自blog.csdn.net/sunyv1/article/details/105840644