微信开放标签wx-open-launch-weapp实现点击跳转到微信小程序

一、微信开放标签使用步骤与微信JS-SDK类似,也需要引入JS文件等步骤。如果是公众号身份的网页,需要绑定安全域名。具体参见微信开放文档

二、vue.config.js配置因为 在vue项目中,直接使用微信开放标签会报错,所以要配置忽略对微信标签的校验,在 vue.config.js 中增加如下配置

module.exports = {
  //...其他配置
  chainWebpack: config => {
    config.module
    .rule('vue')
    .use('vue-loader')
    .tap(options => {
      options.compilerOptions = {
        isCustomElement: tag => tag.startsWith('wx-')
      }
      return options
    })
  }
}

三、在打包后注入的入口文件中引入微信SDK文件

<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js "></script>

四、通过config接口注入权限验证配置并申请所需开放标签

getwxConfig({
    wxMiniId: 'gh_xxxxxx',
    signUrl: window.location.href
}).then(res => {
    const data = res.data.data
    wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
        appId: data.appId, // 必填,公众号的唯一标识
        timestamp: data.timestamp, // 必填,生成签名的时间戳
        nonceStr: data.nonceStr, // 必填,生成签名的随机串
        signature: data.signature, // 必填,签名
        jsApiList: ['updateTimelineShareData'], // 必填,需要使用的JS接口列表
        openTagList: ['wx-open-launch-weapp'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
    })
})

五、注意事项

      所有开放标签都能像普通的HTML标签一样在页面中直接使用,不需要再进行额外的处理。官方文档中写到:

如果所使用的标签允许提供插槽,由于插槽中模版的样式是和页面隔离的,因此需要注意在插槽中定义模版的样式。插槽模版及样式均需要通过<script type="text/wxtag-template"></script>进行包裹。

    但在vue3中,直接在模板中使用<script>标签是会报错的。解决方式如下:

<wx-open-launch-weapp class="opentag" username="gh_xxxxxx" path="pages/cart/cart" @launch="handleLaunchFn" @error="handleErrorFn">
    <div v-is="'script'" type='text/wxtag-template'>
         <div style="width:3rem;height:3rem"></div>
    </div>
</wx-open-launch-weapp>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要触发`wx-open-launch-weapp`跳转,你可以使用以下方法之一: 1. 在代码中直接调用`wx.openBusinessView()`方法。具体代码如下: ```javascript wx.openBusinessView({ businessType: 'wx-open-launch-weapp', extraData: { username: "gh_*****", path: "pages/home/home" }, success: function(res) { // 成功跳转 }, fail: function(res) { // 跳转失败 } }); ``` 在`extraData`中设置小程序的用户名和路径,然后调用`wx.openBusinessView()`方法来触发跳转。 2. 在`<wx-open-launch-weapp>`标签中添加点击事件,并在事件处理函数中调用`wx.navigateToMiniProgram()`方法。具体代码如下: ```html <template> <div class="btn-card"> <h5 class="btn" @click="openMiniProgram">打开小程序</h5> </div> </template> <script> export default { methods: { openMiniProgram() { wx.navigateToMiniProgram({ appId: '', path: 'pages/home/home', success(res) { // 成功跳转 }, fail(res) { // 跳转失败 } }) } } } </script> ``` 在点击事件处理函数`openMiniProgram()`中调用`wx.navigateToMiniProgram()`方法来触发跳转。需要注意在`appId`中填入你的小程序AppID。 无论使用哪种方法,都可以触发`wx-open-launch-weapp`的跳转效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [h5跳转小程序、短信跳转小程序wx-open-launch-weapp>干货满满](https://blog.csdn.net/Angelheca/article/details/125444337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [h5跳转微信小程序 wx-open-launch-weapp](https://blog.csdn.net/qzmlyshao/article/details/131070241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值