H5 Vue跳转小程序

准备工作

  1. 绑定域名

    先登录微信公众平台进入 公众号设置功能设置 里填写 JS接口安全域名

    控制台报 invalid url domain,就是当前地址没配在安全域名里

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

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

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

跳转的Vue页面

javascript 复制代码
<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

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

获取小程序原始ID

获取小程序路径

注意

  1. weixin-jsapiweixin-js-sdk不兼容,用weixin-js-sdk代替
  2. 跳转需要一个认证的公众号作为中介
  3. 需要知道目标小程序的原始ID和页面路径,页面路径必须要进到后台才拿的到,原始ID可以在小程序右上角的三个点那里获取
相关推荐
多看书少吃饭3 小时前
从Vue到Nuxt.js
前端·javascript·vue.js
前端不太难4 小时前
《Vue 项目路由 + Layout 的最佳实践》
前端·javascript·vue.js
老华带你飞4 小时前
物流信息管理|基于springboot 物流信息管理系统(源码+数据库+文档)
数据库·vue.js·spring boot
想学后端的前端工程师4 小时前
【Vue3组合式API实战指南:告别Options API的烦恼】
前端·javascript·vue.js
毕设源码-邱学长6 小时前
【开题答辩全过程】以 基于VUE的藏品管理系统的设计与实现为例,包含答辩的问题和答案
前端·javascript·vue.js
学习CS的小白8 小时前
跨域问题详解
vue.js·后端
周星星日记8 小时前
5.为什么vue中使用query可以保留参数
前端·vue.js
+VX:Fegn08958 小时前
计算机毕业设计|基于springboot + vue作业管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Tzarevich8 小时前
现代前端开发工程化:从 Vite 到 Vue 3 多页面应用实战
vue.js·vite
说私域8 小时前
链动2+1模式AI智能名片S2B2C商城小程序中电商直播的应用机制与价值创新研究
人工智能·小程序