记忆的时间差
记忆的时间差
  • 发布:2021-01-28 14:34
  • 更新:2021-04-04 16:04
  • 阅读:8909

uniapp向webview中发送消息

分类:uni-app

uniapp向webview中发送消息

在业务开发中,使用webview嵌套了vue开发的h5页面,进行套壳打包,其一需求是,在h5中获取当前app的版本号,就涉及到了uniapp向webview传递消息的场景

uniapp 中代码

核心为获取到webview窗口,然后通过evalJS执行h5中的方法

  • setVersion方法为h5页面中定义的全局方法
<template>  
    <view class="content">  
        <!-- 开发 -->  
        <web-view @message="onMessage" :webview-styles="webviewStyles" src="http://192.168.1.158:3000"></web-view>  

    </view>  
</template>  
<script>  
export default {  
    data() {  
        return {  
            title: 'Hello',  
            webviewStyles: {  
                progress: {  
                    color: '#f0b90b'  
                }  
            },  
            wv: null,  
        };  
    },  
    onLoad() {  
        const self = this;  
        // #ifdef APP-PLUS  
        let currentWebview = this.$scope.$getAppWebview();  
        setTimeout(function() {  
            let wv = currentWebview.children()[0];  
            self.wv = wv;  
            self.setVersion();  
        }, 1000);  
        // #endif  

    },  
    methods: {  
        // 设置版本号  
        setVersion() {  
            const self = this;  
            plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
                self.wv.evalJS("setVersion('" + widgetInfo.version + "')");  
            });  
        },  
        // 接收消息  
        onMessage(e) {  
            const self = this;  
            console.log('我收到了网页发来的消息', e.detail.data);  
            let data = e.detail.data;  
        },  
    }  
};  
</script>  

h5 中代码

App.vue

// 设置版本号  
    window.setVersion = (id) => {  
      localStorage.setItem("version", version);  
    };

然后就可以在h5中获取使用了

注意

尽量不要uni生命周期中直接调用h5里面的方法,要等h5加载完成后,才可以调用

1 关注 分享
DCloud_iOS_XHY

要回复文章请先登录注册

阿宁啊

阿宁啊

回复 记忆的时间差 :
贴过来的格式可能有点乱,赋值到hbuider上面应该就好了,然后我这两个都是nvue格式的,vue我也试过了,还是不行
2021-02-04 16:56
阿宁啊

阿宁啊

回复 记忆的时间差 :
大佬,能帮我看下我的demo哪里有问题吗?
index.nvue
```
<template>
<view class="webview-box">
<web-view class="webview" src="/static/index.html"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
webviewStyles: {
progress: {
color: '#f0b90b'
}
},
wv: null,
};
},
onLoad() {
const self = this;
// #ifdef APP-PLUS
let currentWebview = this.$scope.$getAppWebview();
// console.log(currentWebview)
setTimeout(function() {
let wv = currentWebview;
console.log(wv)
self.wv = wv;
}, 1000);
// #endif
setTimeout(function() {
self.wv.evalJS(`uniReslove(1)`);
}, 3000)
}
};
</script>
<style>
.webview-box {
position: absolute;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
}

.webview {
flex: 1;
}
</style>
```

webview里的页面
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>网络网页</title>
<style type="text/css">
.btn {
display: block;
margin: 20px auto;
padding: 5px;
background-color: #007aff;
border: 0;
color: #ffffff;
height: 40px;
width: 200px;
}

.btn-red {
background-color: #dd524d;
}

</style>
</head>
<body>
<div>
<button class="btn btn-red" type="button" id="postMessage">录像并上传</button>
</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">
function uniResolve(res) {
console.log(res)
alert(1)
}
</script>
</body>
</html>
```
2021-02-04 16:55
记忆的时间差

记忆的时间差 (作者)

回复 阿宁啊 :
没有问题,我已经在生产环境使用了,一切正常
2021-02-04 15:21
阿宁啊

阿宁啊

大佬,好像没有生效,获取webview窗口通过evalJS真的能调用H5页面里的函数吗?
2021-02-04 11:51