传参页面:
uni.navigateTo({
url: 'test?id=1&name=uniapp'
});
methods: {
comment:function(id){
uni.navigateTo({
//多个参数用&拼接:url: '../comment/comment?id='+id+'&name='+name
url: '../comment/comment?id='+id
});
},
}
注意:当需要传的参数过多时可以使用es6的模板字符串拼接
var name = 'moss';
var age = 18;
console.log(`My name is ${name} and I am ${age} years old`);
// 打印结果:My name is moss and I am 18 years old
接受参数页面
onLoad:function(option){//opthin为object类型,会序列化上页面传递的参数
console.log(option.id);//打印出上页面传递的参数
}
参数过长
因url有长度限制,太长的字符串会传递失败,可用encodeURIComponent方法来解决
<navigator :url="'/pages/test/test?item='+ encodeURIComponent(JSON.stringify(item))"></navigator>
在test.vue页面接受参数
onLoad(option) {
const item = JSON.parse(decodeURIComponent(option.item));
}