h5跳转微信小程序(微信内部浏览器以及外部浏览器均适用)

1,先把这个链接给后端 让后端写个接口

获取scheme码 | 微信开放文档

需要把path路径(跳到小程序的那个页面的路径)给后端

2,上代码(vue2框架)


<template>
	<button type="button" class="btnGoWeapp" @click="goWeapp">点击跳转小程序</button>
</template>
 <script>
	 	import $ from '../utils/axios'
 export default {
   mounted(){
   },
   methods: {
	   goWeapp(){
        // 先判断是不是pc端打开的h5 如果是pc端提醒用手机打开页面
		   if(window.navigator.userAgent.indexOf("Windows") != -1){
			   window.alert("请使用手机打开!")
		   }else{
			   // 调取接口获取URL Scheme
			   $.post('/api/wx/GenerateScheme', {
			   		path:"pages/store/index",//打开的小程序页面路径
					query:""//传递的参数 在小程序 onload(options) options中可以拿到
			   	})
			   	.then(res => {
					  let UrlScheme=res.Data
					  // 打开小程序
					   window.open(UrlScheme); 
				})
			  
		   }
	   }
  }
 }
</script>
<style scoped>
.btnGoWeapp{
		width: 50%;
		font-size: 0.2rem;
	}
</style>

	

注意:苹果手机可能跳不成功

延迟一点拿到路径再跳

let UrlScheme=res.Data
								   
									   // 打开小程序
								   // window.open(UrlScheme); 
								
								   setTimeout(()=>{
									    window.open(UrlScheme); 
								   },200)

完事~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值