当前位置: 首页 > news >正文

鸿蒙与h5的交互

一 、对象注入,h5调用鸿蒙方法--javaScriptProxy

// xxx.ets
import web_webview from '@ohos.web.webview';class WebViewModel {constructor() {}webCallBack() {console.log('webCallBack')}
}@Entry
@ComponentV2
struct WebComponent {webviewController: web_webview.WebviewController = new web_webview.WebviewController();// 声明需要注册的对象@Local vm: WebViewModel = new WebViewModel();build() {Column() {// web组件加载本地index.html页面Web({ src: $rawfile('test1.html'), controller: this.webviewController }).domStorageAccess(true).javaScriptAccess(true)// 将对象注入到web端.javaScriptProxy({object: this.vm,name: "harmony",methodList: ["webCallBack"],controller: this.webviewController})}}
}html代码:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta content="width=device-width, initial-scale=1.0" name="viewport">
</head><body>
<div class="btn-box"><button class="btn" onclick="callArkTS()" type="button">Click Me!</button><p id="demo"></p>
</div></body>
<script>function callArkTS() {window.harmony.webCallBack();}
</script></html>

二、对象注入,方式二---javaScriptProxy

// xxx.ets
import web_webview from '@ohos.web.webview';@Entry
@ComponentV2
struct WebComponent {webviewController: web_webview.WebviewController = new web_webview.WebviewController();build() {Column() {// web组件加载本地index.html页面Web({ src: $rawfile('test1.html'), controller: this.webviewController }).domStorageAccess(true).javaScriptAccess(true)// 将对象注入到web端.javaScriptProxy({object: {nativeMethod: (channelName: string, paramsCallback: number) => {console.log('paramsCallback' + paramsCallback)if (!channelName || !paramsCallback) {return}if (paramsCallback) {}},},name: "harmony",methodList: ["nativeMethod"],controller: this.webviewController})}}
}html代码:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta content="width=device-width, initial-scale=1.0" name="viewport">
</head><body>
<div class="btn-box"><button class="btn" onclick="callArkTS()" type="button">Click Me!</button><p id="demo"></p>
</div></body>
<script>function callArkTS() {window.harmony.nativeMethod( "callArkTS", "params");}
</script></html>

三、原生调用h5的方法--runJavaScript

// xxx.ets
import web_webview from '@ohos.web.webview';@Entry
@ComponentV2
struct WebComponent {webviewController: web_webview.WebviewController = new web_webview.WebviewController();@Local value1: string = 'value1'@Local value2: string = 'value2'build() {Column() {// web组件加载本地index.html页面Web({ src: $rawfile('test1.html'), controller: this.webviewController }).fileAccess(true).domStorageAccess(true).zoomAccess(false).onPageEnd(() => {this.webviewController.runJavaScript(`nativeFn("${this.value1}","${this.value2}")`)})}}
}html代码:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta content="width=device-width, initial-scale=1.0" name="viewport">
</head><body>
<div class="btn-box"><button class="btn" onclick="callArkTS()" type="button">Click Me!</button><p id="demo"></p>
</div></body>
<script>function nativeFn(value1, value2) {console.log('原生调用' + value1 + '--' + value2);}
</script></html>

相关文章:

  • 交流平台网站怎么做不了百度网络科技有限公司
  • 网站建设如何测试免费seo网站诊断
  • .东莞网站建设百度网盘提取码入口
  • 武昌网站建设高端营销型网站
  • 做网站背景的图杭州seo网站排名
  • 做网站5年工资多少seo站内优化站外优化
  • 基于Kafka实现企业级大数据迁移的完整指南
  • 2025学年湖北省职业院校技能大赛 “信息安全管理与评估”赛项 样题卷(一)
  • 跨线程connect传参的错误
  • Dify、n8n、Coze、FastGPT 和 Ragflow 对比分析:如何选择最适合你的智能体平台?
  • 一款实验室创客实验室用的桌面式五轴加工中心
  • 深入理解残差网络(ResNet):原理与PyTorch实现
  • github 上的php项目
  • java 导出word 实现循环表格
  • Ubuntu 物理桌面远程访问教程(基于 RealVNC / mstsc)
  • npm 报错:“无法加载文件 ...npm.ps1,因为在此系统上禁止运行脚本” 解决方案(附执行策略说明)
  • 暴雨信创电脑代理商成功中标长沙市中医康复医院
  • docker搭建mysql主从集群
  • 笔记01:现有PCB文件自动生成PCB库
  • 分布式系统 - 分布式缓存及方案实现
  • 基于FPGA的UART回环设计
  • Qt开发1--Qt概述,安装,创建第一个Qt项目
  • 在windows系统上安装Comfy UI
  • 内存条与CPU三级缓存之间的区别
  • Vue SPA 路由跳转无法回到顶部问题排查与解决
  • C++设计模式(GOF-23)——04 C++装饰器模式(Decorator)(一个类同时继承和组合另一个类)解决类爆炸问题、模板装饰器