想使用uni.xxx
**.html 需要引入:
https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.0.1.52.js
<!-- uni 的 SDK,必须引用。 -->
<script type="text/javascript" src="//js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.0.1.52.js"></script>
应用的**.vue 通过 web-view 标签引入 **.html
vue文件传值给 html文件 通过 url地址 ?id=1&name=xx 的方式
那html文件如何传给 vue文件呢?
一开始想到的肯定是全局变量什么的吧,但是肯定是不行的
uniapp提供了一个Api用来解决
可看一下官方文档
uni.postMessage(OBJECT)https://uniapp.dcloud.io/component/web-view?id=postmessage
文档对 uni.postMessage 的介绍:
网页向应用发送消息,在
<web-view>
的message
事件回调event.detail.data
中接收消息。
使用详情:
**.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<-- 1. 引入 js 文件,为了可以使用 uni.*** -->
<script type="text/javascript" src="//js.cdn.aliyun.dcloud.net.cn/dev/uni- app/uni.webview.0.1.52.js"></script>
</head>
<body>
<div onclick="to">123</div>
</body>
<script>
// 2. 例如:要在点击 div 时给 vue 传递字符串 'aaa'
to(){
uni.postMessage({
data:'aaa'
})
}
</script>
</html>
**.vue
<template>
<div>
// @message 接收uni.postMessage 回调
<web-view @message='message' src="html的路径"></web-view>
</div>
</template>
<script>
export default{
methods:{
message(e){
// e.detail.data 就是传递的数据
}
}
}
</script>