伊豆
伊豆
  • 发布:2022-03-02 13:46
  • 更新:2022-03-02 16:51
  • 阅读:733

uniapp h5端的与webview的html页面如何交互

分类:uni-app
<template>  
    <view>  
        <web-view ref="minotorModal" v-if="url" :src="url"></web-view>  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                mWeight:uni.upx2px(730),  
                url:''  
            }  
        },  
        onLoad(e) {  
            if(e && e.videoSrc){  
                this.url = '/hybrid/html/monitorVideo.html?videoSrc='+e.videoSrc  
                +"&mW="+encodeURIComponent(this.mWeight);  
            }  
        },  
        onReady() {  
            const that = this;  
            setTimeout(function() {  
                that.doSendMsg();  
            }, 6000);  
        },  
        methods: {  
            doSendMsg(){  
                // #ifdef H5  
                // this.postMessage("hello there!", "*");  
                // if(typeof window.postMessage() === undefined){  
                //  alert("unavailibale");  
                // }else{  
                //  window.postMessage("hello there!", "*");  
                // }  
                this.$refs.minotorModal.postMessage("hello there!", "*");  
                // #endif  

            }  
        }  
    }  
</script>  

<style lang="scss" scoped>  
</style>  

说明:
我看官网是说H5直接使用可以直接使用 window.postMessage,但是我试写了一种方式好像都报错;
(1)、window.postMessage("hello there!", "");
(2)、this.$refs.minotorModal.postMessage("hello there!", "
");
(3)、this.$refs["minotorModal"].contentWindow.postMessage("hello there!", "*");

2022-03-02 13:46 负责人:无 分享
已邀请:
叫啥好呢

叫啥好呢

在/hybrid/html/monitorVideo.html这里 出触发postMessage

  • 伊豆 (作者)

    能否举例怎么写,我是在中途过程中要传数据给html页面,

    2022-03-02 14:39

  • 叫啥好呢

    回复 伊豆: 传给html 用evalJS, html 用postMessage

    2022-03-02 14:41

  • 伊豆 (作者)

    回复 叫啥好呢: 我的应用是公众号(即H5)不是app。调用evalJS,先要获取webview对象,我看大部分都是针对app获取,H5怎么获取不是特别清楚; 我试着this.$refs.minotorModal.evalJS("initBtn('"+JSON.stringify(this.btndata)+"')");这样调用好像不对

    2022-03-02 15:50

[已删除]

[已删除]

好像需要使用uni.$emit和uni.$on来传递了

该问题目前已经被锁定, 无法添加新回复