1. This is the web-view page
<template>
<view>
<!-- 跳转外部网页,这个xxxx地址是h5网页的地址-->
<web-view src="xxxxx" @message="messaged"></web-view>
</view>
</template>
<script>
export default {
methods: {
// 从webview获取参数
messaged(event) {
console.log(event);
},
}
}
</script>
<style></style>
2. This is an h5 page. Don’t forget to import uni’s SDK. I didn’t write uni.navigateBack(), thinking that after executing uni.postMessage(), the @message event of web-view will be received, but it won’t. Add @message receives parameters normally after uni.navigateBack()
<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.0.1.52.js"></script>
<script type="text/javascript">
document.addEventListener('UniAppJSBridgeReady', function () {
uni.postMessage({
data: {
action: '123'
}
});
uni.navigateBack()
});
</script>
-- It is written in the document that the @message event of the web-view will be triggered after executing uni.postMessage(), but it still needs uni.navigateBack() to trigger, or it may be that I misunderstood this sentence in the document. This little thing is really giving me a headache--