鸿蒙与web混合开发双向通信

鸿蒙与web混合开发双向通信用runJavaScript和registerJavaScriptProxy

web

entry/src/main/resources/rawfile/1.html

bash 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>混合开发</title>
</head>
<body>
    <div></div>
    <img src="./a.png" alt="">
    <button onclick="selectImg()">打开相册</button>
</body>
</html>
<script>
    //直接写js代码
    function changeImg(){
        //1.获取img这个元素
        const img = document.querySelector('img')
        //2.修改元素的属性
        img.src = './b.png'
    }
    async function selectImg(){
      //通知鸿蒙,让鸿蒙设备打开相册
       const res = await harmonyDevice.selectImage()
       const div = document.querySelector('div')
       div.innerText = JSON.stringify(res)
    }

</script>

鸿蒙

bash 复制代码
import { webview } from '@kit.ArkWeb'
import { photoAccessHelper } from '@kit.MediaLibraryKit'
import { promptAction } from '@kit.ArkUI'

@Entry
@Component
struct Page {
  webController:WebviewController = new webview.WebviewController()
  selectImg(){
    const picker = new photoAccessHelper.PhotoViewPicker()
    return picker.select()
  }
  build() {
    Column(){
      // 1.想让网页干嘛,直接调网页的JS方法
      Button('鸿蒙-改变网页图片')
        .onClick(()=>{
            // 控制器
            this.webController.runJavaScript('changeImg()')
        })
      // 2.网页控制鸿蒙测
      Button('注入鸿蒙的事件')
        .onClick(()=>{
          promptAction.showToast({
            message:'注入成功'
          })
          // 1.先给网页一个对象,这个对象上包含了所有网页要用到的方法集合
          // 2.给这个对象起一个名字,让网页可以使用这个对象
          // 3.确定网页可以使用的方法集合
          this.webController.registerJavaScriptProxy(
            {
              selectImage:async()=>{
                const res = await this.selectImg()
                return res
              }
            },
            "harmonyDevice",
            ["selectImage"]
          )
        })
      // 1.加载的地址
      // 2.控制
      Button('刷新页面')
        .onClick(()=>{
           this.webController.refresh()
        })
      Web({
        // 本地:
        src:$rawfile('1.html'),
        // src:'https://www.amap.com/',
        controller:this.webController
      })
    }
    .width('100%')
    .height('100%')
  }
}
相关推荐
阿钱真强道5 小时前
10 jetlinks-mqtt-直连设备-属性-读取-返回
linux·服务器·网络·鸿蒙
ITUnicorn1 天前
【HarmonyOS6】从零实现自定义计时器:掌握TextTimer组件与计时控制
华为·harmonyos·arkts·鸿蒙·harmonyos6
晚霞的不甘1 天前
Flutter for OpenHarmony从基础到专业:深度解析新版番茄钟的倒计时优化
android·flutter·ui·正则表达式·前端框架·鸿蒙
向哆哆1 天前
Flutter × OpenHarmony 跨端实战:打造健身俱乐部「数据可视化仪表盘」模块
flutter·信息可视化·开源·鸿蒙·openharmony·开源鸿蒙
小哥Mark1 天前
使用Flutter导航组件TabBar、AppBar等为鸿蒙应用程序构建完整的应用导航体系
flutter·harmonyos·鸿蒙
向哆哆2 天前
打造高校四六级报名管理系统:基于 Flutter × OpenHarmony 的跨端开发实践
flutter·开源·鸿蒙·openharmony·开源鸿蒙
向哆哆2 天前
构建跨端健身俱乐部管理系统:Flutter × OpenHarmony 的数据结构与设计解析
数据结构·flutter·鸿蒙·openharmony·开源鸿蒙
向哆哆2 天前
高校四六级报名管理系统的考试信息模块实现:Flutter × OpenHarmony 跨端开发实践
flutter·开源·鸿蒙·openharmony·开源鸿蒙
程序员清洒2 天前
Flutter for OpenHarmony:Scaffold 与 AppBar — 应用基础结构搭建
flutter·华为·鸿蒙
向哆哆2 天前
高校四六级报名系统通知公告模块实战:基于 Flutter × OpenHarmony 跨端开发
flutter·开源·鸿蒙·openharmony·开源鸿蒙