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可以在小程序右上角的三个点那里获取
相关推荐
2501_916007473 小时前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
会周易的程序员4 小时前
Mermaid Renderer:一款的图表可视化小工具
前端·vue.js·流程图·mermaid
正在走向自律5 小时前
用豆包Seed Evolving打造全功能【AI智能记账】小程序,开源可落地
人工智能·小程序·开源·智能记账
慧一居士19 小时前
Element Plus 按需引入的配置使用说明和完整示例
前端·vue.js
CoderWeen1 天前
我写了个能一步步点着看的 Dijkstra 可视化项目(Vue3 + Leaflet + Generator)
前端·javascript·vue.js
无人生还1 天前
第 1 篇:从 Vue3 到 React 的思维转变
前端·vue.js·react.js
就业发动机1 天前
我用 Tauri 做了一个约 1.5 MB 的开源屏幕标注工具:MarkerOn
vue.js·rust
爱分享的程序猿-Clark1 天前
【前端分享】vue开发项目,如何实现 从A页面跳转到B页面,并传值!
前端·javascript·vue.js
徐小夕1 天前
花了3天,我写了一款开源AI公众号编辑器
前端·vue.js·github
come112341 天前
Vue 2 与 Vue 3 语法区别及使用技巧
前端·javascript·vue.js