在进入的页面加上的onLoad方法,获取参数
onLoad(options) {
if(options.scene) {
let scene = decodeURIComponent(options.scene);
console.log(scene)
// 后续处理scene
}
}
或者,在app.js的onLaunch方法中获取
onLaunch(options) {
if(options.query.scene) {
let scene = decodeURIComponent(options.query.scene);
console.log(scene)
// 后续处理scene
}
}
在开发阶段可以使用微信开发者工具进行场景模拟测试,编译后相当于扫描小程序码进入该页面
如果有多个参数,第一个参数是parent_id=46,第二个参数是object_id=6,
开始我的参数是“parent_id=46&object_id=6”,但是我打印 decodeURIComponent(options.parent_id) 的结果是“46&object_id=6”,打印 decodeURIComponent(options.object_id) 的结果是undefined。
后来改成“scene=46/6”,用“/”或者其它符号隔开,再分别取值。
onLoad(options) {
// 扫码进入小程序
if(options.scene) {
let scene = decodeURIComponent(options.scene);
// '/'是我们定义的参数链接方式
let parent_id = scene.split("/")[0];
let object_id = scene.split('/')[1];
console.log(scene); // 46/6
console.log(parent_id); // 46
console.log(object_id); // 6
}
},
另外,解释一下decodeURIComponent():
定义和用法
decodeURIComponent()函数可对encodeURIComponent()函数编码的URI进行解码。
语法
decodeURIComponent(uriString中)
参数
参数 | 描述 |
uriString中 | 必需。一个字符串,含有编码URI组件或其他要解码的文本。 |