上个文章写过在微信小程序里打开H5页面同时传参,现在介绍一下在H5页面打开微信小程序并回传参数
准备工作
参考 https://caijuan02.github.io/2018/11/23/wxToH5/#more 的准备工作
开始
h5页面引入js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
var user, id;
if (ht.indexOf('name') != -1) {
user = ht.split('?')[1].split('&')[0].split('=')[1];
id = ht.split('?')[1].split('&')[1].split('=')[1];
} else {
user = 'caijuan',
id = '001'
}
$('.box').on('click', function() {
// 跳转小程序接口
wx.miniProgram.navigateTo({
// 要打开的小程序页面和参数
url: `/pages/index/index?name=${user}&id=${id}`
})
// 但是navigateTo有5层 层级限制
})
- 注:该方法只在小程序打开H5页面后,然后再从h5跳到小程序,没有试过在h5直接打开小程序的情况
微信小程序获取参数
在小程序的onLoad生命周期里可以获取 参数在options1
2
3onLoad: function (options) {
console.log(options);
},