使用uniapp写微信小程序,在页面带参传值时,出现了报错Unexpected end of JSON input at JSON.parse (<anonymous>)
错误代码:
a.vue 传递数据页面
const getDetails = (item,index)=> {
let detail = JSON.stringify(item)
uni.navigateTo({
url:'/subPackages/index/B?item='+ detail
});
console.log('item--->',detail);
}
b.vue 接收数据页面
onLoad((e)=>{
let options = JSON.parse(e.item)
}
此时b.vue页面的JSON.parse报错。
解决方法:使用encodeURIComponent()、decodeURIComponent()
解决后的代码:
a.vue 页面
const getDetails = (item,index)=> {
let detail = JSON.stringify(item)
uni.navigateTo({
url:'/subPackages/index/B?item='+ encodeURIComponent(detail)
});
console.log('item--->',detail);
}
b.vue 页面
onLoad((e)=>{
let options = JSON.parse(decodeURIComponent(e.item))
}
此时问题解决!