HarmonyOS H5异步调用arkTs函数,arkTs如何将结果返回至H5 ?

H5 异步调用arkTs函数,artTS如何将结果返回给H5,请提供示例代码。

HarmonyOS
2024-10-28 11:02:10
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以参考如下代码:

import WebView from '@ohos.web.webview';  
  
class Proxy {  
  controller: WebviewController;  
  
  constructor(controller: WebviewController) {  
    this.controller = controller;  
  }  
  
  private init = () => {  
    this.controller.runJavaScript("callback(1111)");  
  }  
  private ipcSendSync = () => {  
    return 'test'  
  }  
  
  public get javaScriptProxy() {  
    return {  
      object: this.perloadModule,  
      name: "testObject",  
      methodList: Object.keys(this.perloadModule),  
      controller: this.controller,  
    }  
  }  
  
  private readonly perloadModule = {  
    init: this.init,  
    ipcSendSync: this.ipcSendSync,  
  }  
}  
  
  
@Component  
struct JsProxy {  
  @State message: string = 'Hello world'  
  private controller: WebviewController = new WebView.WebviewController()  
  private proxy: Proxy = new Proxy(this.controller)  
  
  build() {  
    Column() {  
      Web({ src: $rawfile('jsProxy.html'), controller: this.controller })  
        .width('100%')  
        .height('50%')  
        .javaScriptProxy(this.proxy.javaScriptProxy)  
    }  
    .height('100%')  
    .width('100%')  
    .backgroundColor('rgba(0,0,0,1)');  
  }  
}  
  
class TestProxy {  
  onCallback = (data) => {  
  }  
  
  constructor(onCallback) {  
    this.onCallback = onCallback  
  }  
  
  private init = () => {  
    this.onCallback(12341)  
  }  
}  
  
@Component  
export default struct JsProxy {  
  private controller: WebviewController = new WebView.WebviewController()  
  private testProxy: TestProxy = new TestProxy((data) => {  
    this.controller.runJavaScript(`callback(${data})`);  
  })  
  
  build() {  
    Column() {  
      Web({  
        src: $rawfile('jsProxy.html'),  
        controller: this.controller  
      })  
        .width('100%')  
        .height('50%')  
        .javaScriptProxy({  
          object: this.testProxy,  
          name: "testObject",  
          methodList: ['init'],  
          controller: this.controller,  
        })  
    }  
    .height('100%')  
    .width('100%')  
    .backgroundColor('rgba(0,0,0,1)');  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
分享
微博
QQ
微信
回复
2024-10-28 14:54:34


相关问题
HarmonyOS h5ArkTS通信
811浏览 • 1回复 待解决
HarmonyOS ArkTSH5交互方法
649浏览 • 1回复 待解决
HarmonyOS ArkTSh5的数据解析
546浏览 • 1回复 待解决
H5页面如何ArkTS交互
3712浏览 • 1回复 待解决
HarmonyOS web组件加载h5h5拉起摄像头
1220浏览 • 1回复 待解决
HarmonyOS H5调用原生扫码功能
612浏览 • 1回复 待解决
HarmonyOS H5调用APP图库选择图片
555浏览 • 1回复 待解决
HarmonyOS 本地H5加载
641浏览 • 1回复 待解决
HarmonyOS h5调用系统相机进行拍照
502浏览 • 1回复 待解决
HarmonyOS H5如何访问相册?
798浏览 • 1回复 待解决
HarmonyOS H5桥接
569浏览 • 1回复 待解决
HarmonyOS ArkTS主动与H5通信方式是什么
1128浏览 • 0回复 待解决
HarmonyOS H5和原生交互
643浏览 • 1回复 待解决
HarmonyOS webview h5的localstorage
786浏览 • 1回复 待解决
HarmonyOS web与H5交互
1322浏览 • 1回复 待解决
webview加载Vue h5失败
18356浏览 • 5回复 待解决
HarmonyOS H5页面使用alert函数不生效
405浏览 • 1回复 待解决
提问
该提问已有2人参与 ,帮助了32人