微信小程序 - 扫描小程序码进入小程序并获取参数

在进入的页面加上的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组件或其他要解码的文本。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mossbaoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值