HarmonyOS web通过loadData加载h5标签

web通过loadData加载网页,加入js回调后,h5中ajax请求。现在的问题是加入跟js交互的代码后,评论就加载不了,去掉回调就可以加载评论。

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

​使用DSBridge的封装,可以实现需求。

参考GitHub地址:​https://github.com/751496032/DSBridge-HarmonyOS

修改BaseBridge.ts中的代码如下:​

// private name: string = "_dsbridge" 
private name: string = "xxxx" 
 
// methodList: ['call'], 
methodList: ['openNews'],
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

index.ets代码修改如下:

import web_webview from '@ohos.web.webview' 
import { router } from '@kit.ArkUI'; 
import axios from '@ohos/axios' 
import { WebViewControllerProxy } from '@hzw/ohos-dsbridge' 
import { JsBridge } from '../bridge/JsBridge' 
import { JsBridgeNamespace } from '../bridge/JsBridgeNamespace' 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  controller: WebViewControllerProxy = WebViewControllerProxy.createController(); 
  web_controller: web_webview.WebviewController = new web_webview.WebviewController(); 
  @State htmlData: string = '' 
  // 声明需要注册的对象 
  @State testObj: testClass = new testClass(); 
  build() { 
    Column() { 
      Button('点击请求').onClick(() => { 
        this.web_controller.loadData(this.htmlData, 'text/html', 'utf-8', ' ', ' ') 
      }) 
      Web({ src: '', controller: this.web_controller}) 
        .domStorageAccess(true) 
        .imageAccess(true) 
        .onlineImageAccess(true) 
        .javaScriptAccess(true) 
        .javaScriptProxy(this.controller.getJavaScriptProxy()) 
    } 
    .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]) 
    .width('100%') 
    .height('100%') 
    .alignItems(HorizontalAlign.Center) 
    .justifyContent(FlexAlign.SpaceBetween) 
    .onClick(() => { 
      router.pushUrl({ 
        url: 'pages/CustomDialogUser' 
      }) 
    }) 
  } 
  aboutToAppear(): void { 
    this.controller.addJavascriptObject(new JsBridge()) 
    this.controller.addJavascriptObject(new JsBridgeNamespace(), "namespace") 
    this.controller.setClosePageListener(() => { 
      return true; // false 会拦截关闭页面 
    }) 
    this.getData(); 
  } 
  aboutToDisappear() { 
    this.controller.destroy() 
  } 
  getData() { 
    axios 
      .get('https://xxx.huawei.com.cn/app/v9/news/show?contentid=xxx') 
      .then((res: Object) => { 
        this.htmlData = JSON.parse(JSON.stringify(res)).data.data.data 
      }) 
  } 
} 
class testClass { 
  constructor() { 
  } 
  openNews(gettype: number, targetId: number) { 
    console.log("test", targetId) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-11-07 17:21:00


相关问题
HarmonyOS Web组件加载H5白屏
562浏览 • 1回复 待解决
HarmonyOS web组件加载h5h5拉起摄像头
1220浏览 • 1回复 待解决
HarmonyOS 本地H5加载
641浏览 • 1回复 待解决
HarmonyOS Web组件加载在线H5页面
703浏览 • 1回复 待解决
Web能直接加载h5代码吗?
3674浏览 • 2回复 待解决
HarmonyOS webH5交互
1322浏览 • 1回复 待解决
HarmonyOS Web组件加载本地H5文件跨域
705浏览 • 1回复 待解决
HarmonyOS WebView加载H5卡顿
625浏览 • 1回复 待解决
webview加载Vue h5失败
18356浏览 • 5回复 待解决
HarmonyOS webview加载H5显示过慢
531浏览 • 1回复 待解决
Web中webview和H5交互
1563浏览 • 1回复 待解决
HarmonyOS web原生和H5如何交互?
1168浏览 • 1回复 待解决
HarmonyOS h5 web上传图片,文件的demo
524浏览 • 1回复 待解决