loaddata api注意事项

loaddata api注意事项

HarmonyOS
2024-05-26 17:40:16
1153浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
噜啦噜啦嘞噜啦嘞
// xxx.ets 
import web_webview from '@ohos.web.webview'; 
import business_error from '@ohos.base'; 
 
@Entry 
@Component 
struct WebComponent { 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
 
  build() { 
    Column() { 
      Button('loadData') 
        .onClick(() => { 
          try { 
            this.controller.loadData( 
              "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>", 
              "text/html", 
              "UTF-8" 
            ); 
          } catch (error) { 
            let e: business_error.BusinessError = error as business_error.BusinessError; 
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`); 
          } 
        }) 
      Web({ src: 'www.example.com', controller: this.controller }) 
    } 
  } 
}
  • 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.

上述代码如果html中存在非法字符  例如css中的color:#333  有“#”的时候会加载不了,需要使用文档中提供的加载本地资源的方法,后面两个参数要复制空格。

// xxx.ets 
import web_webview from '@ohos.web.webview'; 
import business_error from '@ohos.base'; 
 
@Entry 
@Component 
struct WebComponent { 
  controller: web_webview.WebviewController = new web_webview.WebviewController(); 
  updataContent: string = '<body><div><image src=resource://rawfile/xxx.png alt="image -- end" width="500" height="250"></image></div></body>' 
 
  build() { 
    Column() { 
      Button('loadData') 
        .onClick(() => { 
          try { 
            this.controller.loadData(this.updataContent, "text/html", "UTF-8", " ", " "); 
          } catch (error) { 
            let e: business_error.BusinessError = error as business_error.BusinessError; 
            console.error(`ErrorCode: ${e.code}, Message: ${e.message}`); 
          } 
        }) 
      Web({ src: 'www.example.com', controller: this.controller }) 
    } 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-05-27 22:23:58


相关问题
ArkTS静态类型开发时的注意事项
3327浏览 • 1回复 待解决
HarmonyOS 使用prefetch4.0有什么注意事项
756浏览 • 1回复 待解决
HarmonyOS多线程安全注意事项:ohos.file.fs
1200浏览 • 1回复 待解决