一、传字符串
传入
let title = 'hello'
uni.navigateTo({
url:`buttonPage/buttonPage?title=${title}`
})
接收
onLoad(option) {
let { title } = option
},
二、传对象
传入
let data = {
title:'hello',
id: 1
}
uni.navigateTo({
url:`buttonPage/buttonPage?data=`+ encodeURIComponent(JSON.stringify(data))
})
接收
url传参时,不转换数据且当参数带有特殊字符的时候,会被截取数据,从而导致数据不完整。因此,想要解决这个问题,需要使用encodeURIComponent对数据进行解码。
onLoad(option) {
let data = JSON.parse(decodeURIComponent(option.data))
console.log(data)
},
使用这两个encodeURIComponent, decodeURIComponent js内置的方法,数据中有%时会报Error in onLoad hook: "URIError: URI malformed的异常bug。
报错原因:路径上所传参数内含有%,浏览器在对“%”执行decodeURIComponent解码时就会报错
解决办法:在传参数编码之前把“%”转换成“%25”,在解码时在更换为%
uni.navigateTo({
url:`buttonPage/buttonPage?data=`+ encodeURIComponent(JSON.stringify(data).replace(/%/g, '%25'))
})
onLoad(option) {
let data = JSON.parse(decodeURIComponent(option.data).replace(/%25/g, '%'))
console.log(data)
}