HarmonyOS 使用Web组件加载页面示例

HarmonyOS  使用Web组件加载页面示例。

HarmonyOS
2024-09-05 10:34:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

1.加载网络资源

import web_webview from '@ohos.web.webview'; 
import business_error from '@ohos.base'; 
import router from '@ohos.router'; 
 
// 1.涉及网络资源获取,需要在module.json5配置ohos.permission.INTERNET网络访问权限 
 
@Entry 
@Component 
struct loadWeb { 
  webviewController:web_webview.WebviewController = new web_webview.WebviewController(); 
  build() { 
    Column(){ 
      Row(){ 
        Image($r('app.media.ic_back')) 
          .width('40vp') 
          .height('40vp') 
          .margin({right:'20vp'}) 
          .onClick(()=>{ 
            router.back() 
          }) 
        // 通过按钮点击加载网络页面 
        Button('Load Web URL') 
          .onClick(()=>{ 
            try{ 
              this.webviewController.loadUrl('https://developer.huawei.com/consumer/cn/'); 
            }catch(error){ 
              let e:business_error.BusinessError = error as business_error.BusinessError; 
              console.error(`ErrorCode:${e.code},Message:${e.message}`); 
            } 
          }) 
          .margin({top:'20vp',bottom:'20vp'}) 
          .flexGrow(2) 
      } 
      .width('100%') 
      .padding({left:'20vp',right:'20vp'}) 
      .border({style:BorderStyle.Solid}) 
 
 
      // 组件创建时,加载网络页面 
      Web({src:'www.huawei.com',controller:this.webviewController }) 
    } 
  } 
}
  • 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.

2.加载本地资源

import web_webview from '@ohos.web.webview'; 
import business_error from '@ohos.base'; 
import router from '@ohos.router'; 
 
@Entry 
@Component 
struct  loadLocalPage{ 
  webviewController:web_webview.WebviewController = new web_webview.WebviewController(); 
 
  build() { 
    Column() { 
      Row(){ 
        Image($r('app.media.ic_back')) 
          .width('40vp') 
          .height('40vp') 
          .margin({right:'20vp'}) 
          .onClick(()=>{ 
            router.back() 
          }) 
        Button('Load Local Page') 
          .onClick(() => { 
            try { 
              this.webviewController.loadUrl($rawfile('loadPage/loadLocalPage/local2.html')) 
            } catch (error) { 
              let e: business_error.BusinessError = error as business_error.BusinessError; 
              console.log(`ErrorCode:${e.code},message:${e.message}`); 
            } 
          }) 
          .margin({top:'20vp',bottom:'20vp'}) 
          .flexGrow(2) 
      } 
      .width('100%') 
      .padding({left:'20vp',right:'20vp'}) 
      .border({style:BorderStyle.Solid}) 
 
      Web({ src: $rawfile('loadPage/loadLocalPage/local1.html'), controller: this.webviewController }); 
    } 
  } 
}
  • 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.

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> 
  <div class="local"> 
  Hello, I am Loacl Page Two! 
  </div> 
  </body> 
  </html> 
 
<style> 
  .local{ 
  width:100%; 
  height:100%; 
  margin:0 auto; 
  font-size:20px; 
  font-weight:700; 
  color:blue; 
  text-align:center; 
} 
</style>
  • 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.
分享
微博
QQ
微信
回复
2024-09-05 16:24:41
相关问题
HarmonyOS web组件 加载web页面异常
1274浏览 • 1回复 待解决
HarmonyOS web组件加载页面空白
1136浏览 • 1回复 待解决
HarmonyOS Web组件无法加载页面
887浏览 • 1回复 待解决
HarmonyOS Web组件加载本地页面传参问题
1277浏览 • 1回复 待解决
使用Web组件加载网页,显示空白。
1428浏览 • 1回复 待解决
HarmonyOS Web组件加载太慢
683浏览 • 1回复 待解决
HarmonyOS Web组件加载在线H5页面
1054浏览 • 1回复 待解决
如何使用Web组件加载本地的html文件?
1817浏览 • 1回复 待解决
HarmonyOS web加载页面图片不显示
1559浏览 • 1回复 待解决
HarmonyOS web交互示例
761浏览 • 1回复 待解决
HarmonyOS web组件加载pdf问题
1565浏览 • 1回复 待解决
Web组件加载在线页面存在2次刷新问题
1360浏览 • 1回复 待解决