【鸿蒙】web 加载vue dist 包里的 html

下面给你「带进度条 + 判断 index.html 已存在则跳过解压」的完整可运行版本,直接替换即可通过 ArkTS 校验。

思路

  1. 先判断 www/dist/index.html 是否存在,存在就直接返回。
  2. 解压前把 unzipFileonProgress 回调接到 @State progress 上,实时刷新 Progress 组件。
  3. 解压完成后隐藏进度条,自动加载本地 html。
ts 复制代码
import web_webview from '@ohos.web.webview';
import fs from '@ohos.file.fs';
import zlib from '@ohos.zlib';
import { Context } from '@ohos.abilityAccessCtrl';

@Entry
@Component
struct WebLoadLocalHtml {
  private webCtrl = new web_webview.WebviewController();
  @State wwwPath: string = '';
  @State showProgress: boolean = false;
  @State progress: number = 0;          // 0~100

  /* 主逻辑:带进度解压 */
  private async copyAndUnzip(context: Context): Promise<string> {
    const wwwDir = context.filesDir + '/www';
    const targetHtml = wwwDir + '/dist/index.html';

    // 1. 已存在则直接返回
    try {
      fs.accessSync(targetHtml);
      console.info('index.html 已存在,跳过解压');
      return wwwDir;
    } catch { /* 不存在就继续 */ }

    // 2. 准备目录
    try { fs.mkdirSync(wwwDir); } catch {}

    // 3. 拷贝 zip
    const rawBytes = context.resourceManager.getRawFileContentSync('dist.zip');
    const zipPath = wwwDir + '/dist.zip';
    this.writeFileOnlyOne(zipPath, rawBytes);

    // 4. 带进度解压
    this.showProgress = true;
    this.progress = 0;

// 3. 解压(补齐第 3 个参数)
let options: zlib.Options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
zlib.unzipFile(destZip, wwwDir, options).then(()=>{
fs.unlinkSync(destZip);
this.wwwPath=wwwDir;
this.showProgress = false;
});

return wwwDir;
}

/* 写文件助手 */
private writeFileOnlyOne(filePath: string, data: Uint8Array) {
try { if (fs.accessSync(filePath)) return; } catch {}
const f = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
fs.writeSync(f.fd, data.buffer);
fs.closeSync(f);
}

/* 生命周期 */
async aboutToAppear() {
this.wwwPath = await this.copyAndUnzip(getContext());
}

build() {
Stack() {
Column() {
Button('加载 html')
.onClick(() => {
this.webCtrl.loadUrl(`file://${this.wwwPath}/dist/index.html`);
})

Web({ src: `file://${this.wwwPath}/dist/index.html`, controller: this.webCtrl })
.javaScriptAccess(true)
.fileAccess(true)
.onPageEnd(() => console.info('Vue 页面加载完成'))
.width('100%')
.height('100%')
}

/* 进度条遮罩 */
if (this.showProgress) {
Column() {
Progress({ value: this.progress, total: 100, type: ProgressType.Linear })
.width('80%')
Text(`解压中...${this.progress}%`)
}
.width('100%')
.height('100%')
.backgroundColor('#80000000')
.justifyContent(FlexAlign.Center)
}
}


}
}

使用要点

  • 判断的是 www/dist/index.html 而不是 www,避免目录存在但文件没写完的误判。
  • 进度条用 Stack 盖在 Web 组件上,解压完自动消失。
相关推荐
一字白首11 分钟前
小程序组件化进阶:从复用到通信的完整指南DAY04
前端·小程序·apache
读忆13 分钟前
你是否用过Tailwind CSS?你是在什么情况下使用的呢?
前端·css·经验分享·笔记·taiiwindcss
阿珊和她的猫17 分钟前
探秘小程序:为何拿不到 DOM 相关 API
前端·小程序
前端不太难18 分钟前
如何设计 AI Native 鸿蒙应用架构
人工智能·架构·harmonyos
弓.长.19 分钟前
ReactNative for OpenHarmony项目鸿蒙化三方库:@react-native-picker
react native·react.js·harmonyos
恋猫de小郭26 分钟前
Android 禁止侧载将正式实施,需要等待 24 小时冷静期
android·flutter·harmonyos
FlyWIHTSKY28 分钟前
Vue 3 onMounted 中控制同步与异步执行策略
前端·javascript·vue.js
ShuiShenHuoLe29 分钟前
组件的状态ComponentV2
harmonyos·鸿蒙
弓.长.30 分钟前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-button — 自定义按钮组件
react native·react.js·harmonyos
PascalMing31 分钟前
告别 Nginx!ASP.NET Core 实现多域名 Vue 静态服务与代理转发
vue.js·nginx·asp.net