【入门到实战】OpenHarmony开发:WebViewComponent实现自定义Web页面

往期鸿蒙5.0全套实战文章必看:(文中附带全栈鸿蒙5.0学习资料)


WebViewComponent实现自定义Web页面

导入依赖

import {WebViewComponent} from '@peakmain/library';

参数

参数参数类型是否必填说明
urlstring跳转的url,默认是空字符串 ""
controllerwebview.WebviewControllerweb控制器,可以控制Web组件各种行为
titlestring标题,根据网页document标题展示对应标题
showTitleboolean显示标题
currentProgressNumbernumber当前进度
onErrorReceive(e: OnErrorReceiveEvent) => void错误回调,默认为空

示例代码

import { router } from '@kit.ArkUI'
import { webview } from '@kit.ArkWeb'
import { NavBar, WebViewComponent } from '../../components'
import { AVOID_AREA_HEIGHT, STATUS_BAR_HEIGHT } from '../../common'
import { WebViewBean } from '../../models'

@Entry({ routeName: "PkWebViewPage" })
@Component
struct PkWebViewPage {
  @StorageProp(STATUS_BAR_HEIGHT) statusBarHeight: number = 0
  @StorageProp(AVOID_AREA_HEIGHT) bottomSafeAreaHeight: number = 0;
  @State url: string = ""
  @State
  controller: webview.WebviewController = new webview.WebviewController()
  @State
  title: string = ""
  @State
  showTitle: boolean = true
  @State
  currentProgressNumber: number = 0
  onErrorReceive?: (e: OnErrorReceiveEvent) => void
  progressColor: ResourceColor = $r("app.color.color_A78461")

  aboutToAppear(): void {
    let bean = router.getParams() as WebViewBean
    this.url = bean.url
    this.showTitle = bean.showTitle
  }

  build() {
    Column() {
      if (this.showTitle) {
        NavBar({
          title: this.title,
          titleColor: Color.Black,
          leftClick: () => {
            if (this.controller.accessBackward()) {
              this.controller.backward()
            } else {
              router.back()
            }
          }
        })
      }
      if (this.currentProgressNumber < 100) {
        Progress({
          value: this.currentProgressNumber
        }).color(this.progressColor)
      }
      WebViewComponent({
        url: this.url,
        controller: this.controller,
        currentProgressNumber: this.currentProgressNumber,
        title: this.title,
        showTitle: this.showTitle,
        onErrorReceive: this.onErrorReceive
      })

    }
    .padding({ top: this.statusBarHeight })
    .margin({
      bottom: this.bottomSafeAreaHeight
    })
    .size({ width: '100%', height: '100%' })
  }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值