请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-webview-V5\#registerjavascriptproxy应用侧代码:import web_view from '@ohos.web.webview' class testClass{ constructor() { } //H5侧调用的方法 test():string{ return '1111111111111111111' } toString():void{ console.log('222222222') } } @Entry @Component struct CallWeb { webviewController:web_view.WebviewController=new web_view.WebviewController(); @State testObj:testClass=new testClass() build() { Column() { Button('将对象注入web').onClick(()=>{ try{ this.webviewController.registerJavaScriptProxy( this.testObj, 'testObjName', ['test','toString']//H5侧可使用的方法 ) } catch (e) { } }) Button('refresh').onClick(()=>{ try{ this.webviewController.refresh() } catch (e) { } }) Web({src:$rawfile('webViewHtml/CallArkTs01.html'),controller:this.webviewController}) } } }H5侧代码:<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <button type="button" onclick="callArkTs()">调用应用侧函数</button> <p id="webPage">Dose Web Call ArkTs Success?</p> <p id="demo"></p> </body> <script> function callArkTs(){ let str = testObjName.test(); //调用HarmonyOS侧test方法 document.getElementById('webPage').innerHTML='callweb1111' document.getElementById('demo').innerHTML=str } </script> </html>
请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-webview-V5\#registerjavascriptproxy
应用侧代码:
H5侧代码: