HarmonyOS 使用@ohos.web.webview封装通用webview组件时基础回调问题

​Demo中的关键类。

CommonWebView 通用WebView组件。

CommonParser 通用回调类。

html调用原生后,无法在CommonParser回调中主动发消息给h5(this.controller.runJavaScript)。

html调用原生后,无法在CommonParser回调中通知界面更新UI。​

HarmonyOS
2024-11-07 10:41:27
723浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa
@Component 
export struct CommonWebView { 
  url: string | Resource=''; 
  controller: WebView.WebviewController = new WebView.WebviewController(); 
  @State pageState: number = 0; //默认为0,暂无数据 
  @State commonParser:CommonParser=new CommonParser(this.controller); 
  @State module: string = ''; 
  @State IsShowLoading: boolean = false; 
  aboutToAppear() { 
    WebView.WebviewController.setWebDebuggingAccess(true); 
  } 
  showLoading() { 
  } 
  hideLoading() { 
  } 
  build() { 
    Row() { 
      Web({ src: this.url, controller: this.controller }) 
        .width(`100%`) 
        .height(`100%`) 
        .javaScriptAccess(true) 
        .domStorageAccess(true) 
        .onControllerAttached(() => { 
          let commonParser = new CommonParser(this.controller); 
          this.controller.registerJavaScriptProxy(commonParser, "objName", ["htmlToNative","setController"]); 
          this.controller.refresh(); 
          // Logger.i('h5页面加载开始 URL: ' + this.controller.getUrl()) 
        }) 
        .onPageEnd((event) => { 
        }) 
        .onErrorReceive(event => { 
          // Logger.e("WebView加载异常 \n URL :" + event.request.getRequestUrl() + '\n error : ' + event.error.getErrorInfo()); 
        }) 
    }.width(`100%`).height(`100%`) 
  } 
} 
class CommonParser { 
  controller: WebView.WebviewController; 
  //module: string; 
  constructor(controller: WebView.WebviewController) { 
    this.controller = controller; 
  } 
  setController(controller: WebView.WebviewController) { 
    this.controller = controller; 
    console.log('sdasda1') 
    return 'aaaaa' 
  } 
  htmlToNative() { 
    //1. html回调原生时无法主动发送消息给h5 
    //this.controller.runJavaScript 
    console.log('sdasda2') 
    //2. html回调原生时无法通知WebView做事情 
  }; 
}
<!-- index.html --> 
  <!DOCTYPE html> 
  <html> 
  <meta charset="utf-8"> 
  <body> 
  <button type="button" onclick="htmlTest()">showDialog!</button> 
  <p id="demo"></p> 
 
  <script type="text/javascript"> 
  function htmlTest() { 
    let str=window.objName.test(); 
    document.getElementById("demo").innerHTML=str; 
    console.log('objName.test result:'+ str) 
  } 
 
  </script> 
  </body> 
 
  </html>
  • 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.
分享
微博
QQ
微信
回复
2024-11-07 16:05:55


相关问题
WebView页面加载错误能力。
1224浏览 • 1回复 待解决
HarmonyOS Web组件
754浏览 • 1回复 待解决
webview静态资源下载完成的
1919浏览 • 1回复 待解决
HarmonyOS web组件停止JavaScript
343浏览 • 1回复 待解决
HarmonyOS webview组件跨域问题
368浏览 • 1回复 待解决
HarmonyOS Scroll组件事件问题
623浏览 • 1回复 待解决
HarmonyOS webview_flutter_ohos加载网页白屏
653浏览 • 1回复 待解决
HarmonyOS 基础控件封装想法
385浏览 • 1回复 待解决
关于HarmonyOS webview中iFrame的使用问题
671浏览 • 1回复 待解决
HarmonyOS webView代理问题
490浏览 • 1回复 待解决
HarmonyOS flutter在ohos如何添加系统
496浏览 • 1回复 待解决
HarmonyOS webview通信问题
423浏览 • 1回复 待解决
HarmonyOS WebView使用自定义键盘问题
477浏览 • 1回复 待解决
恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。