鸿蒙实现 web 传值

[复制链接]
发表于 2025-3-15 08:37:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前言:安卓和 IOS 加载 H5 的时候,都有传值给到 H5 或者吸取 H5 值,鸿蒙也可传值和吸取 H5 的内容,以下是鸿蒙传值给 H5 端的详细操作
一: 界说好 H5 和鸿蒙传值的方法名,两端必须保持方法名划一
// xxx.ets 
import { webview } from '@kit.ArkWeb'
//和 H5 界说的传输内容
class TestObj {
  constructor() {
  }
  test(data1: string, data2: string, data3: string): string {
    console.log("data1:" + data1)
    console.log("data2:" + data2)
    console.log("data3:" + data3)
    return "AceString"
  }
  toString(): void {
    console.log('toString' + "interface instead.")
  }
}
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  controller: webview.WebviewController = new webview.WebviewController()
  testObj = new TestObj();
  build() {
    Row() {
      Column() {
        Web({ src: $rawfile('index.html'), controller: this.controller })
          .javaScriptAccess(true)
          .javaScriptProxy({
            object: this.testObj,  // 传输的内容
            name: "objName", //  H5 界说的传值方法名
            methodList: ["test", "toString","goBack"], // 详细的方法
            controller: this.controller,
          })
      }.width('100%')
    }.height('100%')
  }
}
  1. // H5 页面返回
  2. goBack() {
  3.   router.back()
  4. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表