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可以在小程序右上角的三个点那里获取
相关推荐
猫不易16 小时前
从 Virtual DOM 到 Vapor:Vue 3.6 的另一条路
前端·vue.js
ON.LIN17 小时前
云边去水印小程序详细搭建教程(附源码)
小程序
岁岁种桃花儿17 小时前
Vue核心语法第十二篇:条件渲染
前端·javascript·vue.js
雪碧聊技术20 小时前
Vue + Element Plus 实现文本溢出显示省略号及悬浮提示
前端·javascript·css·vue.js·文本溢出省略
2501_9159090620 小时前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
deng1hao1 天前
2026小微企业企业邮箱选择指南:低成本高性价比方案
网络·科技·小程序
Highcharts.js1 天前
Highcharts 主流前端框架无缝集成指南
前端·vue.js·前端框架·highcharts·可视化图表
岁岁种桃花儿1 天前
Vue核心语法第十一篇:绑定样式
前端·javascript·vue.js
Ai-_Man1 天前
从AI公式到可编辑文档:MathType格式转换技术分析教程
人工智能·ai·小程序
show4331 天前
2026视频处理小程序技术选型指南:链接解析+OCR+ASR+AI配音多引擎对比
小程序·ocr·音视频