外部h5网页无法直接跳转到小程序,因此需要把h5内嵌到小程序的web-view中。只有内嵌的H5才能跳回小程序
官方文档:https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
web-view
承载网页的容器。会自动铺满整个小程序页面,个人类型的小程序暂不支持使用。
一、小程序跳转h5
<!-- wxml -->
<web-view src="https://xxx.com/test.html?id=123"></web-view>
网页放至属性src即可实现自动跳转,需在微信后台设置为白名单
二、h5跳转至小程序
1、将h5内嵌到小程序中,相当于小程序跳转至h5
<view>
<web-view src="https://xxx.com/test.html"></web-view>
</view>
2、在内嵌的网页里引入js,调用wx.miniProgram.navigateTo实现h5跳转到小程序(可在url后拼接要传的参数)
//h5
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
wx.miniProgram.navigateTo({url: '/pages/index/index?shareId=123'})
</script>
这样即可实现从h5跳到小程序(指定页面)
ts:
小程序代码-接收url拼接参数
if(options.shareId){
let shareId = options.shareId /*获取参数*/
that.setData({
shareId: shareId,
})
}