HarmonyOS应用四之页面构建

目录:

1、加载网络页面

在Web组件加载完"www.example.com"页面后,开发者可通过loadUrl接口将此Web组件显示页面变更为"www.example1.com"。

typescript 复制代码
@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Button('loadUrl')
        .onClick(() => {
          try {
            // 点击按钮时,通过loadUrl,跳转到www.example1.com
            this.controller.loadUrl('www.example1.com');
          } catch (error) {
            let e: BusinessError = error as BusinessError;
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`);
          }
        })
      // 组件创建时,加载www.example.com
      Web({ src: 'www.example.com', controller: this.controller})
    }
  }
}

将本地页面文件放在应用的rawfile目录下,开发者可以在Web组件创建的时候指定默认加载的本地页面/html页面数据 ,并且加载完成后可通过调用loadUrl()接口变更当前Web组件的页面。

typescript 复制代码
// xxx.ets
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Button('loadUrl')
        .onClick(() => {
          try {
            // 点击按钮时,通过loadUrl,跳转到local1.html
            this.controller.loadUrl($rawfile("local1.html"));

           // 点击按钮时,通过loadData,加载HTML格式的文本数据
            this.controller.loadData(
              "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>",
              "text/html",
              "UTF-8"
              
          } catch (error) {
            let e: BusinessError= error as BusinessError;
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`);
          }
        })
      // 组件创建时,通过$rawfile加载本地文件local.html
      Web({ src: $rawfile("local.html"), controller: this.controller })
    }
  }
}

2、完善中。。。。。。

相关推荐
世人万千丶1 天前
Flutter 框架跨平台鸿蒙开发 - 恐惧清单应用
学习·flutter·华为·开源·harmonyos·鸿蒙
行乾1 天前
鸿蒙端 IMSDK 架构探索
架构·harmonyos
Utopia^1 天前
Flutter 框架跨平台鸿蒙开发 - 21天挑战
flutter·华为·harmonyos
一直在想名1 天前
Flutter 框架跨平台鸿蒙开发 - 黑白屏
flutter·华为·kotlin·harmonyos
一蓑烟雨,一任平生1 天前
鸿蒙H5调试方法
前端·华为·h5·harmonyos
HwJack201 天前
解密HarmonyOS开发中的LocalStorage是什么
华为·harmonyos
AI_零食1 天前
Flutter 框架跨平台鸿蒙开发 - 孤独指数应用
学习·flutter·开源·harmonyos
浮芷.2 天前
Flutter 框架跨平台鸿蒙开发 - 儿童技能打卡墙应用
科技·flutter·华为·harmonyos·鸿蒙
Utopia^2 天前
Flutter 框架跨平台鸿蒙开发 - 重力感知
flutter·华为·harmonyos
weixin_430750932 天前
AC旁挂+不同区域不同网段+同名wifi同密码 ——实现无线终端智能漫游
网络·华为·无线·漫游