H5 Vue跳转小程序

准备工作

  1. 绑定域名
    先登录微信公众平台进入 公众号设置功能设置 里填写 JS接口安全域名
    控制台报 invalid url domain,就是当前地址没配在安全域名里

  2. 引入JS文件
    npm install weixin-js-sdk

  3. 通过config接口注入权限验证配置

wx.config({
  debug: true, 
  appId: '', // 必填,公众号的唯一标识
  timestamp: , // 必填,生成签名的时间戳,微信相关接口获取
  nonceStr: '', // 必填,生成签名的随机串,微信相关接口获取
  signature: '',// 必填,签名,微信相关接口获取
  jsApiList: [] // 必填,需要使用的JS接口列表(非必填)
});

跳转的Vue页面

<template>
  <div>
  	<!-- demo功能,用时把这个定位到触发跳转的元素上 -->
    <div style="width: 50px; height: 50px; background: red; position: relative">
      <wx-open-launch-weapp
        id="launch-btn"
        username="gh_??????" // 跳转小程序的原始id
        path="pages/index/index" // 跳转小程序的路径
        style="position: absolute; left: 0; top: 0; width: 100%; height: 100%"
      >
        <script type="text/wxtag-template">
          <div style="position: absolute; left: 0; top: 0; width: 100%; height: 100%;" />
        </script>
      </wx-open-launch-weapp>
    </div>
  </div>
</template>

<script>
import wx from 'weixin-js-sdk';

export default {
  methods: {
    getConfig() { 
      this.$http.wechat_api_auth({
          url: location.href.split('#')[0],
        })
        .then(res => {
          if (res.code === '0000') {
            wx.config({
              debug: false,
              appId: res.result.appid,
              timestamp: res.result.timestamp,
              nonceStr: res.result.noncestr,
              signature: res.result.signature,
              jsApiList: [],
              openTagList: ['wx-open-launch-weapp'],
            });
          }
        });
    },
  },

  mounted() {
    this.getConfig();
  },
};
</script>


main.js

// 申明忽略标签 - 用于跳转小程序
Vue.config.ignoredElements = ['wx-open-launch-weapp'];

获取小程序原始ID

在这里插入图片描述


获取小程序路径

在这里插入图片描述

注意

  1. weixin-jsapiweixin-js-sdk不兼容,用weixin-js-sdk代替
  2. 跳转需要一个认证的公众号作为中介
  3. 需要知道目标小程序的原始ID和页面路径,页面路径必须要进到后台才拿的到,原始ID可以在小程序右上角的三个点那里获取
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值