[Harmony]WebView基本用法

module.json5中申请网络权限

TypeScript 复制代码
{
  "module": {
    "requestPermissions": [
      {
        "name": "ohos.permission.INTERNET", // 网络权限
        "reason": "$string:internet_reason",
        "usedScene": {
          "abilities": [],
          "when": "always"
        }
      }
    ]
  }
}
javascript 复制代码
{
  "string": [
    {
      "name": "internet_reason",
      "value": "网络访问"
    }
  ]
}

跳转WebView时传值

TypeScript 复制代码
Text() {
  Span("我已阅读并同意")
    .fontColor($r('app.color.mf_base_333333'))
    .fontSize(15)
  Span("《用户隐私协议》")
    .fontColor($r('app.color.mf_base_yellow'))
    .fontSize(15)
    .onClick(() => this.userPrivacyPolicy())
}
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(2)
.layoutWeight(1)  

/// 用户隐私协议
private userPrivacyPolicy() {
  router.pushUrl({
    url: 'pages/features/protocol/MFProtocolView',
    params: {
      webUrl: 'https://www.baidu.com',
      title: '隐私协议'
    }
  })
}

WebView

TypeScript 复制代码
import { router } from '@kit.ArkUI';
import { webview } from '@kit.ArkWeb';

interface MFProtocolParams {
  webUrl: string;
  title?: string;
}

@Entry
@Component
struct MFProtocolView {
  @State webUrl: string = ''; // 接收的网页地址
  @State title: string = '协议详情'; // 导航栏标题
  private controller: webview.WebviewController = new webview.WebviewController();

  aboutToAppear() {
    // 在aboutToAppear生命周期中读取路由参数
    const params: MFProtocolParams = router.getParams() as MFProtocolParams;
    if (params) {
      this.webUrl = params.webUrl || 'about:blank'; // 默认为空页面
      this.title = params.title || this.title; // 默认为'协议详情'
    }
  }

  build() {
    Column() {
      // 导航栏
      Row({ space: 8 }) {
        Image($r('app.media.icon_base_back')) // 返回图标
          .objectFit(ImageFit.Contain)
          .width(24)
          .height(24)
          .onClick(() => {
            router.back(); // 返回上一页
          })

        Text(this.title) // 标题
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
      .padding(12)
      .backgroundColor('#F1F3F5')

      // 必须传递完整WebOptions参数
      Web({
        src: this.webUrl,
        controller: this.controller 
      })
        .onPageBegin((event) => {
          console.log('开始加载:' + event.url);
        })
    }
    .width('100%')
    .height('100%')
    .onAppear(()=>{
      console.log('onAppear:' + this.webUrl);
    })
  }
}

示意图

相关推荐
Georgewu2 小时前
ArkGenie 的技术原理:从一段 SSE 到一个可交互 Surface
harmonyos
Georgewu2 小时前
在 HarmonyOS 上做一套真正能接入应用的 AI 对话组件:ArkGenie 项目介绍
harmonyos
2501_944525543 小时前
开源鸿蒙平台 KMP_CMP 三方库「EitherNet」适配全流程
华为·开源·harmonyos
程序猿追4 小时前
返回 null 不一定入队成功:鸿蒙 KMP上的并发优先队列观察笔记
笔记·华为·harmonyos·kmp
2501_944525544 小时前
开源鸿蒙平台 KMP_CMP 三方库「kotlinx.immutable」适配全流程
华为·开源·harmonyos
2501_944525767 小时前
开源鸿蒙平台 KMP_CMP 三方库「Kaml」适配全流程
开源·kotlin·harmonyos
2501_944525548 小时前
开源鸿蒙平台 KMP_CMP 三方库「Clikt」适配全流程
华为·开源·harmonyos
特立独行的猫a9 小时前
用仓颉写桌面应用的“薄壳框架”cj-tauri ,以及它的 AI 对话客户端 ai-chat
人工智能·harmonyos·tauri·仓颉·鸿蒙pc·cj-tarui·ai客户端
万物智能信息科技10 小时前
Zigbee透传驱动开发—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
驱动开发·华为·开源·harmonyos·鸿蒙
牛哥带你学代码11 小时前
鸿蒙远程控制电脑最新体验:华为荣耀鸿蒙用户2026远控软件选择指南
华为·电脑·harmonyos