h5打开小程序,h5跳转到小程序

业务背景:在h5中直接跳转到小程序(注意不是返回,是直接打开)

直接上码:
一、config注入:

wx.config({
     
 debug: true,
 appId: '',
 timestamp: '',
 nonceStr: '',
 signature: '',
 jsApiList: [ 'onMenuShareTimeline','onMenuShareAppMessage'],
 openTagList: ['wx-open-launch-weapp']
});

二、前端代码:

<wx-open-launch-weapp
id="launch-btn"
username="小程序原始id"
path="小程序页面地址"
style="width:100px;height:100px;"
>
<template>
<style>.btn {
      padding: 12px;width:100px;height:80px;background:skyblue; }</style>
<button class="btn">打开小程序<button>
</template>
</wx-open-launch-weapp>
<script>
var btn = document.getElementById('launch-btn');
btn.addEventListener('launch', function (e) {
     
console.log('success');
});
btn.addEventListener('error', function (e) {
     
console.log('fail', e.detail);
});
</script>

※目前仅支持通过微信浏览器访问h5打开小程序

你可能感兴趣的:(web前端,前端,javascript,html5)