j***@163.com
j***@163.com
  • 发布:2023-05-11 18:14
  • 更新:2023-07-03 10:32
  • 阅读:574

uniapp向webview中的页面发送消息,H5页面不更新问题

分类:uni-app

uniapp 里面定义了webview 加载H5

                          init() {  
                let that = this  
                                // this.u 是h5页面地址  
                // #ifdef APP-PLUS  
                wv = plus.webview.create(this.u, '', {  
                    top: this.topHeight,  
                    bottom: '120px'  
                })  
                var currentWebview = this.$scope.$getAppWebview();  
                currentWebview.append(wv);  
                plus.globalEvent.addEventListener('plusMessage', function(msg) {  
                    if (msg.data.args.data.name == 'postMessage') {  
                        that.handlePostMessage(msg.data.args.data)  
                    }  
                });  
                // #endif  
            }  

用wv.evalJS("getResult('" + JSON.stringify(res) + "')") 向H5发送消息,H5页面用vue写的代码:


<template>  
    <div id="app" class="hello">  
        <h3>点击按钮进行测试</h3>  
        <div ref="div1" id="div1">  
            获取到的内容:{{result}}  
        </div>  
    </div>  
</template>  
<script>  
export default {  
    name: 'HelloWorld',  
    data() {  
        return {  
            result: '1'  
        }  
    },  
    mounted() {  
        window.getResult = this.getResult  
    },  
    methods: {  
        getResult(res) {  
          this.result = res  
          alert(this.result)  
        }  
    }  
}  
</script>  

这个时候 getResult alert可以显示结果,alert(this.result) 也能看到改变,但是div页面无法更新,尝试过vue的nexttick和forceupdate 都无任何效果,这种情况有遇到吗?

2023-05-11 18:14 负责人:无 分享
已邀请:
昭昭L

昭昭L - 开心就好

h5的数据变了视图没变?强制更新下视图

你的名字2020

你的名字2020

这是this指向问题吧,window.getResult = this.getResult.bind(this)试试看呢

要回复问题请先登录注册