HarmonyOS4.0系统性深入开发38Web组件概述

news/2024/7/21 12:02:03 标签: harmonyos, microsoft, 鸿蒙, 开发语言

Web组件概述

Web组件用于在应用程序中显示Web页面内容,为开发者提供页面加载、页面交互、页面调试等能力。

  • 页面加载:Web组件提供基础的前端页面加载的能力,包括加载网络页面、本地页面、Html格式文本数据。
  • 页面交互:Web组件提供丰富的页面交互的方式,包括:设置前端页面深色模式,新窗口中加载页面,位置权限管理,Cookie管理,应用侧使用前端页面JavaScript等能力。
  • 页面调试:Web组件支持使用Devtools工具调试前端页面。

下面通过常见使用场景举例,来具体介绍Web组件功能特性。

Web组件概述

Web组件用于在应用程序中显示Web页面内容,为开发者提供页面加载、页面交互、页面调试等能力。

  • 页面加载:Web组件提供基础的前端页面加载的能力,包括加载网络页面、本地页面、Html格式文本数据。
  • 页面交互:Web组件提供丰富的页面交互的方式,包括:设置前端页面深色模式,新窗口中加载页面,位置权限管理,Cookie管理,应用侧使用前端页面JavaScript等能力。
  • 页面调试:Web组件支持使用Devtools工具调试前端页面。

下面通过常见使用场景举例,来具体介绍Web组件功能特性。

设置深色模式

Web组件支持对前端页面进行深色模式配置。

  • 通过

    darkMode()

    接口可以配置不同的深色模式,

    WebDarkMode.Off

    模式表示关闭深色模式。

    WebDarkMode.On

    表示开启深色模式,并且深色模式跟随前端页面。

    WebDarkMode.Auto

    表示开启深色模式,并且深色模式跟随系统。

    在下面的示例中, 通过darkMode()接口将页面深色模式配置为跟随系统。

    // xxx.ets
    import web_webview from '@ohos.web.webview';
    
    @Entry
    @Component
    struct WebComponent {
      controller: web_webview.WebviewController = new web_webview.WebviewController();
      @State mode: WebDarkMode = WebDarkMode.Auto;
      build() {
        Column() {
          Web({ src: 'www.example.com', controller: this.controller })
            .darkMode(this.mode)
        }
      }
    }
    
  • 通过

    forceDarkAccess()

    接口可将前端页面强制配置深色模式,且深色模式不跟随前端页面和系统。配置该模式时候,需要将深色模式配置成WebDarkMode.On。

    在下面的示例中, 通过forceDarkAccess()接口将页面强制配置为深色模式。

    // xxx.etsimport web_webview from '@ohos.web.webview';
    @Entry@Componentstruct WebComponent {  controller: web_webview.WebviewController = new web_webview.WebviewController();  @State mode: WebDarkMode = WebDarkMode.On;  @State access: boolean = true;  build() {    Column() {      Web({ src: 'www.example.com', controller: this.controller })        .darkMode(this.mode)        .forceDarkAccess(this.access)    }  }}
    

上传文件

Web组件支持前端页面选择文件上传功能,应用开发者可以使用onShowFileSelector()接口来处理前端页面文件上传的请求。

下面的示例中,当用户在前端页面点击文件上传按钮,应用侧在onShowFileSelector()接口中收到文件上传请求,在此接口中开发者将上传的本地文件路径设置给前端页面。

  • 应用侧代码。

    // xxx.ets
    import web_webview from '@ohos.web.webview';
    @Entry
    @Component
    struct WebComponent {
      controller: web_webview.WebviewController = new web_webview.WebviewController()
      build() {
        Column() {
          // 加载本地local.html页面
          Web({ src: $rawfile('local.html'), controller: this.controller })
            .onShowFileSelector((event) => {
                // 开发者设置要上传的文件路径
               let fileList: Array<string> = [
                  'xxx/test.png',
               ]
               if(event){
                  event.result.handleFileList(fileList)
               }
               return true;
            })
        }
      }
    }
    
  • local.html页面代码。

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Document</title>
    </head>
    
    <body>
    // 点击文件上传按钮
    <input type="file" value="file"></br>
    </body>
    </html>
    

    在新窗口中打开页面

    Web组件提供了在新窗口打开页面的能力,开发者可以通过harmonyos-references-V2/ts-basic-components-web-0000001477981205-V2#ZH-CN_TOPIC_0000001523968730__multiwindowaccess9" rel="nofollow">multiWindowAccess()接口来设置是否允许网页在新窗口打开。当有新窗口打开时,应用侧会在onWindowNew()接口中收到Web组件新窗口事件,开发者需要在此接口事件中,新建窗口来处理Web组件窗口请求。

    说明

    • 如果开发者在onWindowNew()接口通知中不需要打开新窗口,需要将ControllerHandler.setWebController()接口返回值设置成null。

    如下面的本地示例,当用户点击“新窗口中打开网页”按钮时,应用侧会在onWindowNew()接口中收到Web组件新窗口事件。

    • 应用侧代码。

      // xxx.ets
      import web_webview from '@ohos.web.webview'
      
      //在同一page页有两个web组件。在WebComponent新开窗口时,会跳转到NewWebViewComp。
      @CustomDialog
      struct NewWebViewComp {
      controller?: CustomDialogController
      webviewController1: web_webview.WebviewController = new web_webview.WebviewController()
      build() {
          Column() {
            Web({ src: "", controller: this.webviewController1 })
              .javaScriptAccess(true)
              .multiWindowAccess(false)
              .onWindowExit(()=> {
                console.info("NewWebViewComp onWindowExit")
                if (this.controller) {
                  this.controller.close()
                }
              })
            }
        }
      }
      
      @Entry
      @Component
      struct WebComponent {
          controller: web_webview.WebviewController = new web_webview.WebviewController()
          dialogController: CustomDialogController | null = null
          build() {
            Column() {
              Web({ src:$rawfile("window.html"), controller: this.controller })
                .javaScriptAccess(true)
               //需要使能multiWindowAccess
                .multiWindowAccess(true)
                .onWindowNew((event) => {
                if (this.dialogController) {
                  this.dialogController.close()
                }
                let popController:web_webview.WebviewController = new web_webview.WebviewController()
                this.dialogController = new CustomDialogController({
                  builder: NewWebViewComp({webviewController1: popController})
                })
                this.dialogController.open()
                //将新窗口对应WebviewController返回给Web内核。
                //如果不需要打开新窗口请调用event.handler.setWebController接口设置成null。
                //若不调用event.handler.setWebController接口,会造成render进程阻塞。
                event.handler.setWebController(popController)
              })
          }
        }
      }
      
    • window.html页面代码。

       <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8">
          <title>WindowEvent</title>
      </head>
      <body>
      <input type="button" value="新窗口中打开网页" onclick="OpenNewWindow()">
      <script type="text/javascript">
          function OpenNewWindow()
          {
              let openedWindow = window.open("about:blank", "", "location=no,status=no,scrollvars=no");
              openedWindow.document.write("<p>这是我的窗口</p>");
              openedWindow.focus();
          }
      </script>
      </body>
      </html>
      

管理位置权限

Web组件提供位置权限管理能力。开发者可以通过onGeolocationShow()接口对某个网站进行位置权限管理。Web组件根据接口响应结果,决定是否赋予前端页面权限。获取设备位置,需要开发者配置ohos.permission.LOCATION权限。

在下面的示例中,用户点击前端页面"获取位置"按钮,Web组件通过弹窗的形式通知应用侧位置权限请求消息,示例代码如下:

  • 前端页面代码。

    <!DOCTYPE html>
    <html>
    <body>
    <p id="locationInfo">位置信息</p>
    <button onclick="getLocation()">获取位置</button>
    <script>
    var locationInfo=document.getElementById("locationInfo");
    function getLocation(){
      if (navigator.geolocation) {
        <!-- 前端页面访问设备地理位置 -->
        navigator.geolocation.getCurrentPosition(showPosition);
      }
    }
    function showPosition(position){
      locationInfo.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude;
    }
    </script>
    </body>
    </html>
    
  • 应用代码。

    <!DOCTYPE html>
    <html>
    <body>
    <p id="locationInfo">位置信息</p>
    <button onclick="getLocation()">获取位置</button>
    <script>
    var locationInfo=document.getElementById("locationInfo");
    function getLocation(){
      if (navigator.geolocation) {
        <!-- 前端页面访问设备地理位置 -->
        navigator.geolocation.getCurrentPosition(showPosition);
      }
    }
    function showPosition(position){
      locationInfo.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude;
    }
    </script>
    </body>
    </html>
    

http://www.niftyadmin.cn/n/5394947.html

相关文章

JAVA学习-控制执行流程.for

在Java中&#xff0c;for循环是一种常用的控制执行流程的循环语句。它允许我们重复执行一段代码&#xff0c;直到满足指定的循环条件。 一、for循环的基本语法如下&#xff1a; for (初始化语句; 循环条件; 循环后操作) {// 循环体&#xff0c;要执行的代码} 其中&#xff0c…

记录西门子:实物ST60与Factory IO测试1

主程序&#xff1a; 子程序&#xff1a; IO映射 Factory IO配置: MCGS触摸屏&#xff1a;

Stable Diffusion下载完身体有点吃不消(内附秋叶集合安装包)

大家好&#xff01;今天我来介绍一款令人惊叹的开源软件——Stable Diffusion。这是一款基于AI技术的图片生成软件&#xff0c;可以让用户轻松随心所欲地创建出各种令人惊叹的图片。 在这里插入图片描述 Stable Diffusion拥有强大的AI算法&#xff0c;不管是风景、人物、动物…

七、ChatGPT为什么会被热炒?

2023年上半年&#xff0c;ChatGPT引起了广泛的热议&#xff0c;对于ChatGPT有多热&#xff0c;不需要我重复了&#xff0c;你可能在网上看到了很多报道&#xff0c;标题如《ChatGPT揭开AI战幔&#xff1a;杀死黄页一样摧毁Google&#xff1f;》和《ChatGPT强势来袭&#xff0c;…

Golang 关于 interface 接口的理解

package mainimport "fmt"// 定义一个存储器接口&#xff1a;支持mysql存储、redis存储 type StorageManager interface {insert(data string) int // 增加update(id int, data string) int // 更新 }// 实现一个Mysql存储器 type Mysql struct{}func (mysql…

gitlab添加ssh公钥

一&#xff1a;生成公钥 桌面鼠标右击打开 Open Git Bash here (前提是安装了Git)&#xff1b; 2.输入命令 ssh-keygen -t rsa -C "123*****90qq.com"来生成新的密钥对,将其中的"123*****90qq.com"替换为你自己的电子邮件地址。 命令&#xff1a;ssh-keyg…

SQL进阶(三):Join 小技巧:提升数据的处理速度

复杂数据结构处理&#xff1a;Join 小技巧&#xff1a;提升数据的处理速度 本文是在原本sql闯关的基础上总结得来&#xff0c;加入了自己的理解以及疑问解答&#xff08;by GPT4&#xff09; 原活动链接 用到的数据&#xff1a;链接 提取码&#xff1a;l03e 目录 1. 课前小问…

第 3 章 ROS通信机制(自学二刷笔记)

重要参考&#xff1a; 课程链接:https://www.bilibili.com/video/BV1Ci4y1L7ZZ 讲义链接:Introduction Autolabor-ROS机器人入门课程《ROS理论与实践》零基础教程 3.1.3 回旋函数 C 在ROS程序中&#xff0c;频繁的使用了 ros::spin() 和 ros::spinOnce() 两个回旋函数&…