- I encountered such a problem in the process of using uniapp to pass parameters. When we need to pass the entire object as a parameter, I will encode the object first, and then decode it , so as to obtain the parameters. It is not encountered in normal practice. I have encountered the problem, but when I was testing today, it happened that a data happened to be wrong, and the error content was:
Error in onLoad hook: “URIError: URI malformed“ found in
It turns out that uniapp encodes ( encodeURIComponent ) and decodes ( decodeURIComponent ) for object parameter passing as follows:
//跳转前页面的方法
toDeviceDetail(deviceInfo) {
//传参对象,使用encodeURIComponent编码
let query = encodeURIComponent(JSON.stringify(deviceInfo))
uni.navigateTo({
url:'/pages/……/deviceDetail?deviceQuery='+ query
})
},
// 跳转后页面获取的参数的方法
onLoad(options) {
let obg ={}
// 获取传递的对象参数,使用decodeURIComponent解码,并转为对象
obj = JSON.parse(decodeURIComponent(options.deviceQuery));
}
- The bug discovered today is that if the parameter in the encoded object has "%", then it will cause problems for encoding and decoding, and an error will be reported
- Solution: Decode normally, process all "%" separately during encoding, and manually convert to "%25", the code is as follows:
toDeviceDetail(deviceInfo) {
//传参对象,使用encodeURIComponent编码
let str = JSON.stringify(deviceInfo)
//注意这里——————————————————————
str = str.replace(/%/g, '%25')
//这里———————————————————————————
let query = encodeURIComponent(JSON.stringify(deviceInfo))
uni.navigateTo({
url:'/pages/……/deviceDetail?deviceQuery='+ query
})
},
- Finally realize the need to jump pages and pass object parameters in uni-app