a***@fuweex.com
a***@fuweex.com
  • 发布:2025-02-18 13:42
  • 更新:2025-02-21 10:14
  • 阅读:178

鸿蒙webview通信evalJS,不支持this.$scope.$getAppWebview().children()[0],需要使用使用uni.createWebviewContext

分类:鸿蒙Next

APP往webview通过evalJS注入方法时
鸿蒙不支持this.$scope.$getAppWebview().children()[0] ,使用uni.createWebviewContext

//template  
<web-view id="webviewId" src="xxxxxx"></web-view>  
//javascript  

// #ifdef APP-HARMONY  
this.currentWebview = uni.createWebviewContext('webviewId', this);  
// #endif  
// #ifdef APP-PLUS  
this.currentWebview = this.$scope.$getAppWebview().children()[0];  
// #endif  
let token = uni.getStorageSync('token');  
let tenant = uni.getStorageSync('tenantCode');  
// 准备要传递的数据  
let dataToPass = JSON.stringify({  
    token: token  
});  
this.currentWebview.evalJS(`uniappReceiveData('${JSON.stringify({ token: token,tenant: tenant})}')`)
0 关注 分享

要回复文章请先登录注册

a***@fuweex.com

a***@fuweex.com (作者)

回复 1***@qq.com :
https://ask.dcloud.net.cn/article/35083
2025-02-21 10:14
a***@fuweex.com

a***@fuweex.com (作者)

回复 1***@qq.com :
```
<script>
console.log('function uniappReceiveData')
function uniappReceiveData(res) {
console.log('uniappReceiveData-------------------' + res);

if(res) {
try {
const data = JSON.parse(res);
// 设置token和tenant
sessionStorage.setItem('ACCESS_TOKEN', data.token);
sessionStorage.setItem('tenant', data.tenant);
document.cookie = `tenant=${data.tenant}`;

// 可以添加日志便于调试
console.log('Token set successfully in uniappReceiveData');
} catch (err) {
console.error('处理 uniapp 数据时出错:', err);
}
}
};
</script>
```
在h5页面定义这个方法,先h5给app postMessage 发消息,然后app接受到消息再evaJS执行h5的方法
2025-02-21 10:11
1***@qq.com

1***@qq.com

顺便问下,h5如何接收app端的方法呢
2025-02-19 09:23
Adlaw

Adlaw

uni.createWebviewContext('webview', this).evalJs() 鸿蒙平台要用这种方法来获取并发送JS
2025-02-18 14:27