微信小程序webview与html页面传参

适用场景

        在微信小程序中强制阅读pdf(预览pdf可参考pdf.js),html端返回阅读状态。

实现思路

1.小程序端:使用<web-view ></web-view>标签来承载pdf链接。
2.html端:可通过计时器强制阅读10s,10s后点击我已阅读按钮,返回阅读状态。

代码

html端

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
		<title></title>
		<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
	</head>
	<body>
		<div id="app">
			<button type="button" @click="post_message()">点击向小程序传递消息</button>
		</div>
		//引入小程序JSSDK
		<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
		<script type="text/javascript">
			var vm = new Vue({
				el:"#app",
				data:{
					msg:"hello"
				},
				methods:{
					post_message(){
						wx.miniProgram.navigateBack({
							delta: 0
						})
						wx.miniProgram.postMessage({data:this.msg})
					}
				},
			})
		</script>
	</body>
</html>

//网页中使用JSSDK提供的接口返回小程序页面
//通过postMessage方法向小程序端返回参数

小程序端

<web-view src="http://127.0.0.1:8848/myFirstTest/test1.html" bindmessage="get_message"></web-view>
//通过e.detail获得返回结果
 get_message(e){
        console.log(e.detail )
    },
//src属性为链接地址
//bindmessage 接收html端通过postMessage方法返回的的参数

运行结果
html
在小程序控制台输出html端返回的msg

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失忆症患者_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值