根據(jù)微信的開放標簽wx-open-launch-weapp可以實現(xiàn)從h5頁面跳轉小程序的需求
微信文檔地址:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html
一、安裝weixin-js-sdk
版本要求1.6.0
npm install weixin-js-sdk
二、config注入
wx.config({
debug: true,// 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。
appId: appId, // 必填,公眾號的唯一標識,填自己的!
timestamp: timestamp, // 必填,生成簽名的時間戳,剛才接口拿到的數(shù)據(jù)
nonceStr: nonceStr, // 必填,生成簽名的隨機串
signature: signature, // 必填,簽名,見附錄1
jsApiList: [
'wx-open-launch-weapp',
],
openTagList: ["wx-open-launch-weapp"] // 跳轉小程序時必填
})
let _this = this
wx.ready(function (res) {
console.log(res)
})
wx.error(function (res) {
// config信息驗證失敗會執(zhí)行error函數(shù),如簽名過期導致驗證失敗,具體錯誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對于SPA可以在這里更新簽名
console.log(res)
});
三、頁面引入
<wx-open-launch-weapp
class="openweapp"
id="launch-btn"
username="gh_c001fa831731"
path="pages/index/index.html"
>
<script type="text/wxtag-template">
<style>.btn { padding: 12px;opacity:0 }</style>
<button class="btn">打開小程序</button>
</script>
</wx-open-launch-weapp>
mounted() {
var btn = document.getElementById("launch-btn");
btn.addEventListener("launch", function (e) {
console.log("success");
});
btn.addEventListener("error", function (e) {
alert("小程序打開失敗");
console.log("fail", e.detail);
});
},
四、vue報錯
vue會顯示wx-open-launch-weapp組件未注冊
在main.js中加入
Vue.config.ignoredElements = ['wx-open-launch-weapp']
五、效果展示

9005739c46c226971939fefbb388b45.jpg