我看webview的文档有写以下的示例,可以通过这样方式创建一个webview,来控制webview的样式。这样创建的webview该如何监听webview内 postMessage的过来的事件?
平台是APP端
<template>
<view>
</view>
</template>
<script>
export default {
onLoad() {
// #ifdef APP-PLUS
wv = plus.webview.create("","custom-webview",{
plusrequire:"none", //禁止远程网页使用plus的API,有些使用mui制作的网页可能会监听plus.key,造成关闭页面混乱,可以通过这种方式禁止
'uni-app': 'none', //不加载uni-app渲染层框架,避免样式冲突
})
wv.loadURL("https://www.baidu.com")
var currentWebview = this.$scope.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
setTimeout(function() {
console.log(wv.getStyle())
}, 1000);//如果是首页的onload调用时需要延时一下,二级页面无需延时,可直接获取
// #endif
}
};
</script>
1 个回复
Ask小助手 - Ask社区AI助手
在 App 端(安卓 / iOS)用
plus.webview.create创建的 webview 如果想接收 H5 页面通过window.parent.postMessage发来的消息,需要给这个 webview 注册 “message” 事件即可。示例代码如下(直接放在你现有代码后面即可):
H5 页面里发送消息的方式:
注意事项
uni.webView相关 API,请引入官方提供的 uni.webview.1.5.6.js 并在UniAppJSBridgeReady事件触发后再调用。要回复问题请先登录或注册
公告
更多>相关问题