local.html代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>本地网页</title>
<style type="text/css"></style>
</head>
<body>
<div id="vue-scope"></div>
<!-- uni 的 SDK -->
<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
<script type="text/javascript">
// 接收vue传递的参数
function getInfo(data) {
console.log(data);
}
document.addEventListener('UniAppJSBridgeReady', function() {
// html给vue传值
uni.postMessage({
data: {
action: 'message'
}
});
uni.getEnv(function(res) {
console.log('当前环境:' + JSON.stringify(res));
});
})
</script>
</body>
</html>
web-view-local.vue代码
<template>
<view>
<web-view src="/hybrid/html/local.html" @message="getMessage"></web-view>
</view>
</template>
<script>
export default {
methods: {
/**
* 接收html传递的数据
*/
getMessage(e) {
console.log(e.detail.data[0].action);
},
/**
* 调用html中的getInfo方法并传值
*/
postInfo() {
// #ifdef APP-PLUS
let webViews = this.$scope.$getAppWebview();
setTimeout(function() {
let currentWebview = webViews.children()[0];
let data = '123';
currentWebview.evalJS("getInfo('" + data + "')");
}, 100);
// #endif
}
}
}
</script>