由于小程序官方沒有提供外部H5網頁直接跳轉到小程序的api怀挠,所以目前只支持小程序內嵌H5,并且只有內嵌的H5才能跳回小程序
小程序跳轉H5
需要用到小程序的web-view,官方文檔鏈接
web-view是承載網頁的容器。會自動鋪滿整個小程序頁面,個人類型的小程序暫不支持使用补君。寫法如下:
<view class="page-body">
<web-view src="https://xxx.com/test.html"></web-view>
</view>
注:當在微信開發(fā)中工具里返回“{"base_resp":{"ret":-1}}”時,需要點左上角“設置”--“項目設置”--勾選“不校驗合法域名昧互、web-view(業(yè)務域名)挽铁、TLS 版本以及 HTTPS 證書”
image.png
H5跳轉小程序
因為外部h5無法跳轉到小程序,因此需要把h5內嵌到小程序的web-view中敞掘。
一:首頁小程序內嵌h5網頁,內嵌這一步就相當于上面的小程序跳轉h5:
<view class="page-body">
<web-view src="https://xxx.com/test.html"></web-view>
</view>
二:然后在內嵌的網頁里引入js叽掘,調用wx.miniProgram.navigateTo跳轉小程序方法,可在url后拼接要傳的參數:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>h5跳轉小程序</title>
</head>
<body>
<h3 align="center">正在跳轉到小程序...</h3>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
wx.miniProgram.navigateTo({url: '/index/index?phone=18012121212'})
</script>
</body>
</html>
三:小程序接受參數的頁面:
index.wxml:
<view class="page-body">
{{phone}}
</view>
index.js
Page({
data: {
phone:''
},
onLoad: function (options) {
var that = this;
/*獲取參數*/
var phone = options.phone
that.setData({
phone: phone,
})
}
})
這樣就從h5跳到小程序指定的頁面并且可以拿到我們想要傳的參數
image.png
關于web-view相關的接口:
image.png
官方js調用方法示例:
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
// javascript
wx.miniProgram.navigateTo({url: '/path/to/page'})
wx.miniProgram.postMessage({data: 'foo'})
wx.miniProgram.postMessage({data: {foo: 'bar'}})
wx.miniProgram.getEnv(function (res) { console.log(res.miniprogram) })