h5跳小程序(uniapp,vue)wx-open-launch-weapp

<template>
    <div style="width: 100%; height:160rpx;">
        <wx-open-launch-weapp
		username="gh_xxxxxxxxx"          
		path="/pages/index/index.html"
		>       
		<script type="text/wxtag-template"> 
			<view style="font-size: 40px;">去查看(跳转小程序)</view>      
			</script>     
		</wx-open-launch-weapp> 
    </div>
</template>

<script>
import Api from '@/api/test';
export default {
	name:"tiao",
    methods: {
        wxmini(){
        	// 获取密钥
            Api.getSignPackage({url:window.location.href}).then(res=>{
                wx.config({ // eslint-disable-line
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
                    appId:res.data.data.appId, // 必填,公众号的唯一标识
                    timestamp:res.data.data.timestamp, // 必填,生成签名的时间戳
                    nonceStr:res.data.data.nonceStr, // 必填,生成签名的随机串
                    signature:res.data.data.signature,// 必填,签名
                    jsApiList: ['onMenuShareTimeline'], // 必填,需要使用的JS接口列表
                    openTagList: ['wx-open-launch-weapp'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
                });
                /* eslint-disable */
                wx.ready(function () {
                    console.log('ready')
                });
            })
        }
    },
    created() {
		
        const oScript = document.createElement('script');
        oScript.type = 'text/javascript';
        oScript.src = 'https://res2.wx.qq.com/open/js/jweixin-1.6.0.js';
        oScript.onload = this.wxmini
        document.body.appendChild(oScript);
    },
}
</script>

<style>
</style>

效果为

注:此处真机才显示按钮,且安卓正常显示

iOS很多新版本不显示 其中有两种方法 

1,一般刷新页面就可以显示(有效 但项目不一定允许)

2,用v-html渲染

注意</script>在其中要用‘\’进行转义  写为: <\/script>  

<template>
    <div style="width: 100%;" v-html="html">
         
    </div>
</template>

<script>
import Api from '@/api/test';
export default {
	data() {
		return {
			html:''
		}
	},
	name:"tiao",
    methods: {
        wxmini(){
        	// 获取密钥
            Api.getSignPackage({url:window.location.href}).then(res=>{
                wx.config({ // eslint-disable-line
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
                    appId:res.data.data.appId, // 必填,公众号的唯一标识
                    timestamp:res.data.data.timestamp, // 必填,生成签名的时间戳
                    nonceStr:res.data.data.nonceStr, // 必填,生成签名的随机串
                    signature:res.data.data.signature,// 必填,签名
                    jsApiList: ['onMenuShareTimeline'], // 必填,需要使用的JS接口列表
                    openTagList: ['wx-open-launch-weapp'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
                });
                /* eslint-disable */
                wx.ready(function () {
                    console.log('ready')
                });
            })
        }
    },
    created() {
        this.html=
        `<wx-open-launch-weapp
          username="gh_XXXXXXXXXX"          
          path="/pages/index/index.html"
          >       
          <script type="text/wxtag-template"> 
            <view style="font-size: 40px;">去查看(跳转小程序)</view>      
            <\/script>     
          </wx-open-launch-weapp>
          `
        const oScript = document.createElement('script');
        oScript.type = 'text/javascript';
        oScript.src = 'https://res2.wx.qq.com/open/js/jweixin-1.6.0.js';
        oScript.onload = this.wxmini
        document.body.appendChild(oScript);
    },
}
</script>

<style>
</style>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Vuewx-open-launch-weapp组件来跳转小程序,您可以参考以下完整代码: ```html <template> <wx-open-launch-weapp username="gh_*******" path="pages/******/******.html" id="open-launch" v-if="phoneAuth" @launch="onLaunch" @error="onError" > <script type="text/wxtag-template"> <style> .btn { width: 92px; height: 92px; } </style> <div class="btn"></div> </script> </wx-open-launch-weapp> </template> <script> export default { data() { return { phoneAuth: true }; }, methods: { onLaunch() { console.log('小程序跳转成功'); }, onError(error) { console.log('小程序跳转失败', error); } } }; </script> ``` 在这个代码中,我们使用了Vuewx-open-launch-weapp组件来实现小程序跳转。需要注意的是,你需要将`username`和`path`属性替换为你自己小程序的用户名和路径。在`v-if`中,我们使用了`phoneAuth`来判断是否需要展示跳转按钮。 在`@launch`事件中,我们定义了跳转成功后的回调函数`onLaunch`,在`@error`事件中,我们定义了跳转失败后的回调函数`onError`。 这样,当用户点击按钮后,如果跳转成功,你就会在控制台看到"小程序跳转成功"的输出。如果跳转失败,你就会在控制台看到"小程序跳转失败"的输出,并且可以通过`error`参数获取具体的错误信息。 希望这个代码对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue使用wx-open-launch-weapp跳转小程序](https://blog.csdn.net/Mr_C_C/article/details/119926287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值