鸿蒙 应用端与web端JS交互

// 在前端页面中调用应用侧方法。
index.ets

import { webview } from '@kit.ArkWeb';

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";

  }

  asyncTest(data: string): void {

    console.log("async data:" + data);

  }

  toString(): void {

    console.log('toString' + "interface instead.");

  }

}

@Entry

@Component

struct WebComponent {

  controller: webview.WebviewController = new webview.WebviewController();

  testObj = new TestObj();

  build() {

    Column() {

      Web({ src: 'www.example.com', controller: this.controller })

        .javaScriptAccess(true)

        .javaScriptProxy({

          object: this.testObj, 

          name: "objName", // 注册对象的名称,与window中调用的对象名一致

          methodList: ["test", "toString"], // test, toSstring 与web端约定的同步方法名

          asyncMethodList: ["asyncTest"],  // asyncTest, 与web端约定的异步方法名 (非必填)

          controller: this.controller,

      })

    }

  }

}

//web端代码

<!-- index.html -->

<!DOCTYPE html>

<html>

<body>

<button type="button" onclick="callArkTS()">Click Me!</button>

<p id="demo"></p>

<script>

    function callArkTS() {

        let str = testObjName.test();

        document.getElementById("demo").innerHTML = str;

        console.info('ArkTS Hello World! :' + str);

    }

</script>

</body>

</html>

//在应用侧调用前端函数

index.ets

import { webview } from '@kit.ArkWeb';

@Entry

@Component

struct WebComponent {

  webviewController: webview.WebviewController = new webview.WebviewController();

  aboutToAppear() {

    // 配置Web开启调试模式

    webview.WebviewController.setWebDebuggingAccess(true);

  }

  build() {

    Column() {

      Button('runJavaScript')

        .onClick(() => {

          // 前端页面函数无参时,将param删除。

          this.webviewController.runJavaScript('htmlTest(param)');

        })

      Button('runJavaScriptCodePassed')

        .onClick(() => {

          // 传递runJavaScript侧代码方法。

          this.webviewController.runJavaScript(`function changeColor(){document.getElementById('text').style.color = 'red'}`);

        })

      Web({ src: $rawfile('index.html'), controller: this.webviewController })

    }

  }

}

//前端代码

<!-- index.html -->

<!DOCTYPE html>

<html>

<body>

<button type="button" onclick="callArkTS()">Click Me!</button>

<h1 id="text">这是一个测试信息,默认字体为黑色,调用runJavaScript方法后字体为绿色,调用runJavaScriptCodePassed方法后字体为红色</h1>

<script>

    // 调用有参函数时实现。

    var param = "param: JavaScript Hello World!";

    function htmlTest(param) {

        document.getElementById('text').style.color = 'green';

        console.log(param);

    }

    // 调用无参函数时实现。

    function htmlTest() {

        document.getElementById('text').style.color = 'green';

    }

    // Click Me!触发前端页面callArkTS()函数执行JavaScript传递的代码。

    function callArkTS() {

        changeColor();

    }

</script>

</body>

</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容