当前位置:   article > 正文

鸿蒙开发组件之Web_鸿蒙的web组件必须借助模拟器调试吗

鸿蒙的web组件必须借助模拟器调试吗

一、加载一个url

  1. myWebController: WebviewController = new webview.WebviewController
  2. build() {
  3. Column() {
  4. Web({
  5. src: 'https://www.baidu.com',
  6. controller: this.myWebController
  7. })
  8. }
  9. .width('100%')
  10. .height('100%')
  11. }

二、注意点

2.1 不能用Previewer预览

Web这个组件不能使用预览,只能使用模拟器或者真机查看。

2.2 需要网络权限

需要在module.json5配置网络权限

  1. "requestPermissions": [
  2. {
  3. "name" : "ohos.permission.INTERNET",
  4. //注意这里有个系统bug,不能写reason
  5. // 'reason': 'abc',
  6. "usedScene" : {
  7. "when" : "inuse"
  8. },
  9. }
  10. ],

2.3 加载的不是当前页面

默认加载的是Index页面,我们需要在EntryAbility.ts的onWindowStageCreate方法中,设置加载咱们的Web的page。

  1. onWindowStageCreate(windowStage: window.WindowStage) {
  2. // Main window is created, set main page for this ability
  3. hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  4. //加载咱们的WebPage页面
  5. windowStage.loadContent('pages/WebPage', (err, data) => {
  6. if (err.code) {
  7. hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  8. return;
  9. }
  10. hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
  11. });
  12. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/307057
推荐阅读