一、简单参数传参
参数为字符串、数值、布尔等基础数据类型
多个参数之间使用&符号分割
uni.navigateTo({
url:"/pages-hotel/roomTypeDetail/index?id='123456789'&name='中秋礼盒'"
})
跳转页面接收参数
onLoad(options){
console.log(options); // {id='123456789',name='中秋'}
var id = options.id;
var name= options.name;
}
二、复杂参数传参
参数为对象、数组等
参数需使用encodeURIComponent方法编码
var i = {
id:'123456789',
price:599,
name:'中秋礼盒',
num:8,
ingredient:['鸡蛋','面粉','花生','芝麻'],
};
uni.navigateTo({
url:"/pages/XXX/index?detailData=" + encodeURIComponent(JSON.stringify(i))
})
跳转页面接收参数
onLoad(options){
console.log(options); // {detailData='%5Bobject%20Object%5D'}
var detailData = JSON.parse(decodeURIComponent(options.detailData));
}
扩展
encodeURIComponent()
-
encodeURIComponent()
函数通过将特定字符的每个实例替换成代表字符的 UTF-8 编码的一个、两个、三个或四个转义序列来编码 URI -
与 encodeURI() 相比,此函数会编码更多的字符,包括 URI 语法的一部分
decodeURIComponent()
-
decodeURIComponent()
方法用于解码由 encodeURIComponent 方法或者其他类似方法编码的部分统一资源标识符(URI)