深圳网站建 1设骏域网站建设,wordpress博客统计小工具,陕西交通建设集团公司网站,想要导航网站推广怎么做Web组件概述
Web组件用于在应用程序中显示Web页面内容#xff0c;为开发者提供页面加载、页面交互、页面调试等能力。
页面加载#xff1a;Web组件提供基础的前端页面加载的能力#xff0c;包括加载网络页面、本地页面、Html格式文本数据。页面交互#xff1a;Web组件提供…Web组件概述
Web组件用于在应用程序中显示Web页面内容为开发者提供页面加载、页面交互、页面调试等能力。
页面加载Web组件提供基础的前端页面加载的能力包括加载网络页面、本地页面、Html格式文本数据。页面交互Web组件提供丰富的页面交互的方式包括设置前端页面深色模式新窗口中加载页面位置权限管理Cookie管理应用侧使用前端页面JavaScript等能力。页面调试Web组件支持使用Devtools工具调试前端页面。
下面通过常见使用场景举例来具体介绍Web组件功能特性
使用Web组件加载页面
页面加载是Web组件的基本功能。根据页面加载数据来源可以分为三种常用场景包括加载网络页面、加载本地页面、加载HTML格式的富文本数据。
加载网络页面 开发者可以在Web组件创建的时候指定默认加载的网络页面 。在默认页面加载完成后如果开发者需要变更此Web组件显示的网络页面可以通过调用loadUrl()接口加载指定网络网页。
在下面的示例中在Web组件加载完“www.example.com”页面后开发者可通过loadUrl接口将此Web组件显示页面变更为“www.example1.com”。
// xxx.ets
import web_webview from ohos.web.webview;Entry
Component
struct WebComponent {webviewController: web_webview.WebviewController new web_webview.WebviewController();build() {Column() {Button(loadUrl).onClick(() {try {// 点击按钮时通过loadUrl跳转到www.example1.comthis.webviewController.loadUrl(www.example1.com);} catch (error) {console.error(ErrorCode: ${error.code}, Message: ${error.message});}})// 组件创建时加载www.example.comWeb({ src: www.example.com, controller: this.webviewController})}}
}加载本地页面
将本地页面文件放在应用的rawfile目录下开发者可以在Web组件创建的时候指定默认加载的本地页面 并且加载完成后可通过调用loadUrl()接口变更当前Web组件的页面。
在下面的示例中展示加载本地页面文件的方法
将资源文件放置在应用的resources/rawfile目录下。图1 资源文件路径 应用侧代码
// xxx.ets
import web_webview from ohos.web.webview;Entry
Component
struct WebComponent {webviewController: web_webview.WebviewController new web_webview.WebviewController();build() {Column() {Button(loadUrl).onClick(() {try {// 点击按钮时通过loadUrl跳转到local1.htmlthis.webviewController.loadUrl($rawfile(local1.html));} catch (error) {console.error(ErrorCode: ${error.code}, Message: ${error.message});}})// 组件创建时通过$rawfile加载本地文件local.htmlWeb({ src: $rawfile(local.html), controller: this.webviewController })}}
}local.html页面代码。
!-- local.html --
!DOCTYPE html
htmlbodypHello World/p/body
/html加载HTML格式的文本数据 Web组件可以通过loadData接口实现加载HTML格式的文本数据。当开发者不需要加载整个页面只需要显示一些页面片段时可通过此功能来快速加载页面。
// xxx.ets
import web_webview from ohos.web.webview;Entry
Component
struct WebComponent {controller: web_webview.WebviewController new web_webview.WebviewController();build() {Column() {Button(loadData).onClick(() {try {// 点击按钮时通过loadData加载HTML格式的文本数据this.controller.loadData(htmlbody bgcolorwhiteSource:presource/pre/body/html,text/html,UTF-8);} catch (error) {console.error(ErrorCode: ${error.code}, Message: ${error.message});}})// 组件创建时加载www.example.comWeb({ src: www.example.com, controller: this.controller })}}
}最后
本文主要讲解了鸿蒙的Web组件简单介绍和使用更多的Harmony开发技术可以查看我主页学习更多技术类容。下面附上一份鸿蒙开发学习路线图略缩图【详细高清图】可以找我拿一下。