当前位置: 首页 > 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>

http://www.xdnf.cn/news/1068661.html

相关文章:

  • 机器学习01
  • IDEA高效开发指南:JRebel热部署
  • 每日AI资讯速递 | 2025-06-25
  • Django
  • (C++)vector数组相关基础用法(C++教程)(STL库基础教程)
  • 7.Spring框架
  • TensorFlow Lite (TFLite) 和 PyTorch Mobile模型介绍1
  • 什么是功能测试和非功能测试?
  • Azure 托管 Redis 已正式发布
  • 打造属于你的AI智能体,从数据开始 —— 使用 Bright Data MCP+Trae快速构建垂直智能体
  • 【Elasticsearch】es初识,在项目架构中的用途,与mysql和kafka的配合使用,
  • linux安装docker
  • LE AUDIO---Chapter 2. The Bluetooth® LE Audio architecture
  • 记一次AWS 中RDS优化费用使用的案例
  • 【学习笔记】3.3 Decoder-Only PLM
  • 【目标检测】平均精度(AP)与均值平均精度(mAP)计算详解
  • 从数据到决策:UI前端如何利用数字孪生技术提升管理效率?
  • list的使用和模拟实现
  • 探索解析C++ STL中的 list:双向链表的高效实现与迭代器
  • Flutter MobX 响应式原理与实战详解
  • OpenLayers 上传Shapefile文件
  • docker start mysql失败,解决方案
  • 【猜你需要】使用了Git LFS还是无法上传文件?文件过大?
  • 打造丝滑的Android应用:LiveData完全教程
  • 【启发式算法】RRT*算法详细介绍(Python)
  • Oracle 数据库查询:多表查询
  • 测试平台ui自动化demo说明
  • [论文阅读] 人工智能 + 软件工程 | 探秘LLM软件代理:从黑箱决策到透明轨迹分析
  • 创客匠人 AI 赋能:创始人 IP 打造的效率革命与信任重构
  • 数的范围(连续数字边界)