移动应用开发中,网页使用的场景非常多,比如在APP内安排一个优惠活动啥的,就可以直接加载一个H5页面高效并且及时,也省去了使用原生开发要升级版本的麻烦,ArkUI开发框架提供了 Web 组件来加载一个网页,本节笔者简单介绍一下 Web 组件的用法。
interface WebInterface {
(value: WebOptions): WebAttribute;
}
declare interface WebOptions {
src: string | Resource;
controller: WebviewController;
}使用 Web 组件时,需要传递一个 WebOptions 类型的参数, WebOptions 类型说明如下:
Web 组件各种行为,一个 WebController 对象只能控制一个 Web 组件,且必须在 Web 组件和 WebController 绑定后,才能调用 WebController 上的方法。简单样例如下所示:
import webview from '@ohos.web.webview';
@Entry @Component struct WebTest {
// Web控制器
private webController: WebviewController = new webview.WebviewController();
build() {
Column({ space: 10 }) {
Text("www.arkui.club")
.fontSize(25)
.backgroundColor(Color.Pink)
Web({
src: "https://www.arkui.club", // 默认加载 www.arkui.club 网址
controller: this.webController
})
.width("100%")
.height("100%")
}
.width('100%')
.height("100%")
.padding(10)
}
}declare class WebAttribute extends CommonMethod<WebAttribute> {
javaScriptAccess(javaScriptAccess: boolean): WebAttribute;
fileAccess(fileAccess: boolean): WebAttribute;
onlineImageAccess(onlineImageAccess: boolean): WebAttribute;
domStorageAccess(domStorageAccess: boolean): WebAttribute;
imageAccess(imageAccess: boolean): WebAttribute;
mixedMode(mixedMode: MixedMode): WebAttribute;
javaScriptProxy(javaScriptProxy: { object: object, name: string, methodList: Array<string>, controller: WebController }): WebAttribute;
databaseAccess(databaseAccess: boolean): WebAttribute;
userAgent(userAgent: string): WebAttribute;
// 省略部分方法
}rawfile 路径的文件, 默认为 false,表示不启用。JavaScript 对象到 window 对象中,并在 window 对象中调用该对象的方法。所有参数不支持更新。📢: Web 组件的属性方法比较多,笔者仅仅介绍常用的,有关更多属性的用法,读者请自行查阅文档。
declare class WebAttribute extends CommonMethod<WebAttribute> {
onPageBegin(callback: (event?: { url: string }) => void): WebAttribute;
onPageEnd(callback: (event?: { url: string }) => void): WebAttribute;
onProgressChange(callback: (event?: { newProgress: number }) => void): WebAttribute;
onTitleReceive(callback: (event?: { title: string }) => void): WebAttribute;
onAlert(callback: (event?: { url: string, message: string, result: JsResult }) => boolean): WebAttribute;
onConsole(callback: (event?: { message: ConsoleMessage }) => boolean): WebAttribute;
onErrorReceive(callback: (event?: { request: WebResourceRequest, error: WebResourceError }) => void): WebAttribute;
onFileSelectorShow(callback: (event?: { callback: Function, fileSelector: object }) => void): WebAttribute;
}newProgress 的取值范围为0 ~ 100。document 标题更改时触发该回调。alert() 时触发该回调。console() 方法时的回调。input 标签的 type 为 flie 时,点击按钮触发该回调。WebController 用来控制Web组件的各种行为,一个 WebController 对象只能控制一个 Web 组件,且必须在 Web 组件和 WebController 绑定后,才能调用 WebController 上的方法。
declare class WebController {
constructor();
onInactive(): void;
onActive(): void;
clearHistory(): void;
runJavaScript(options: { script: string, callback?: (result: string) => void });
loadData(options: { data: string, mimeType: string, encoding: string, baseUrl?: string, historyUrl?: string });
loadUrl(options: { url: string | Resource, headers?: Array<Header> });
accessBackward(): boolean;
accessForward(): boolean;
accessStep(step: number): boolean;
backward();
forward();
// 省略部分方法
}Web 组件进入未激活状态。Web 组件进入激活状态。loadUrl() 完成后,比如 onPageEnd() 中调用。import webview from '@ohos.web.webview';
let url_data = `
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
<script type='text/javascript'>
function test() {
alert('button click');
}
</script>
</head>
<body>
<button onclick='test()' type='button'>Button Click</button>
</body>
</html>
`
@Entry @Component struct WebTest {
private webController: webview.WebviewController = new webview.WebviewController();
build() {
Column({ space: 10 }) {
Row({space: 10}) {
Button("刷新")
.onClick(() => {
this.webController.refresh();
})
Button("加载本地资源")
.onClick(() => {
this.webController.loadData(
url_data,
"text/html",
"utf-8"
)
})
Button("执行本地JS代码")
.onClick(() => {
this.webController.runJavaScript("test()");
})
}
Row({space: 10}) {
Button("前进")
.onClick(() => {
this.webController.forward();
})
Button("后退")
.onClick(() => {
this.webController.backward();
})
Button("清除记录")
.onClick(() => {
this.webController.clearHistory();
})
}
Web({
src: "https://www.arkui.club", // 默认加载 www.arkui.club 网址
controller: this.webController
})
.width("100%")
.height("100%")
}
.width('100%')
.height("100%")
.padding(10)
}
}本节笔者简单介绍了 Web 组件的使用,由于 Web 组件提供的方法非常多,笔者没办法把这些方法都一一列举出来,读者可执行查阅官方文档熟悉各方法的用法。
如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。