HarmonyOS Web组件如何加载wasm文件

在项目资源目录rawfile文件夹内置rawfile/index.html、rawfile/lib/libpag.min.js和rawfile/lib/libpag.wasm文件,其中index.html引入了js文件,在js中加载了wasm文件,使用web组件加载index.html报错如何解决

报错信息:

[CONSOLE:6719] “Fetch API cannot load resource://rawfile/webpag/lib/libpag.wasm. URL scheme “resource” is not supported.”, source: resource://rawfile/webpag/lib/libpag.min.js (6719)
[CONSOLE:7091] “TypeError: Failed to fetch”, source: resource://rawfile/webpag/lib/libpag.min.js (7091)
[CONSOLE:80] “Uncaught (in promise) ReferenceError: buffer is not defined”, source: resource://rawfile/webpag/index.html (80)
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以参考如下示例:

import webview from '@ohos.web.webview';

@Entry
@Component
struct MainPage {
  @State isLoading: boolean = true
  @State msg: string = ""
  controller: webview.WebviewController = new webview.WebviewController();

  aboutToAppear() {
    webview.WebviewController.setWebDebuggingAccess(true);
  }

  build() {
    Row() {
      Column() {
        Stack() {
          Web({ src: "https://xxx/index.html", controller:this.controller })
            .backgroundColor(Color.White)
            .multiWindowAccess(true)
            .javaScriptAccess(true)
            .geolocationAccess(true)
            .imageAccess(true)
            .onlineImageAccess(true)
            .domStorageAccess(true)
            .fileAccess(true)
            .mediaPlayGestureAccess(true)
            .mixedMode(MixedMode.All)
            .onAlert((event) => {
              return false
            })
            .onProgressChange((event) => {
              if (event?.newProgress == 100) {
                this.isLoading = false
              }
            })
            .onConsole((event) => {
              return false;
            })
            .onInterceptRequest((event) => {
              if (!event) {
                return;
              }
              let requestUrl:string = event.request.getRequestUrl();
              console.log('请求为:'+event.request.getRequestUrl())
              interface HtmlMimeType{
                type:string,
                mime:string
              }
              let typeArr :Array<HtmlMimeType> = [{
                type:".html",
                mime:"text/html"
              },{
                type:".js",
                mime:"text/javascript"
              },{
                type:".css",
                mime:"text/css"
              },{
                type:".wasm",
                mime:"application/wasm"
              }];
              let isSearch:boolean = false;
              let searchTypeObj : HtmlMimeType;
              for (const typeObj of typeArr) {
                let index:number = requestUrl.lastIndexOf(typeObj.type);
                if (index > 0) {
                  isSearch = true;
                  searchTypeObj = typeObj;
                  break;
                }
              }
              if (isSearch) {
                let rawfileName :string = requestUrl.replace("https://xxx/","");
                let endIndex = rawfileName.indexOf("?");
                if (endIndex > - 1) {//如果没去除,文件名会导致错误
                  rawfileName = rawfileName.substring(0,endIndex);
                }
                let response = new WebResourceResponse();
                response.setResponseData($rawfile(rawfileName));
                response.setResponseEncoding('utf-8');
                response.setResponseMimeType(searchTypeObj!.mime);
                response.setResponseCode(200);
                response.setReasonMessage('OK');
                response.setResponseIsReady(true);
                return response;
              }
              return null;
            })
            .width('100%')
            .height('100%')
            .backgroundColor("#ffeef5ee")

          if (this.isLoading) {
            LoadingProgress()
              .width(40)
              .height(40)
          }

        }
        .alignContent(Alignment.Center)
        .width('100%')
        .height('100%')
      }
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS Web组件加载html文件异常
746浏览 • 1回复 待解决
web组件对html文件加载
993浏览 • 1回复 待解决
HarmonyOS web组件如何加载本地字库?
439浏览 • 1回复 待解决
HarmonyOS web组件 加载web页面异常
406浏览 • 1回复 待解决
HarmonyOS web加载沙箱文件失败
228浏览 • 1回复 待解决
HarmonyOS Web组件加载太慢
204浏览 • 1回复 待解决
HarmonyOS Web组件HTTP图片加载如何放行
685浏览 • 1回复 待解决
HarmonyOS web组件加载pdf问题
929浏览 • 1回复 待解决
如何禁止web组件自动加载图片?
919浏览 • 1回复 待解决
HarmonyOS web组件如何注入js文件
176浏览 • 1回复 待解决
HarmonyOS Web组件如何加载html字符串
893浏览 • 1回复 待解决
HarmonyOS Web组件无法加载页面
219浏览 • 1回复 待解决
HarmonyOS web组件加载页面空白
344浏览 • 1回复 待解决
HarmonyOS Web组件加载pdf预览
489浏览 • 1回复 待解决
HarmonyOS web组件加载url失败
711浏览 • 1回复 待解决
HarmonyOS Web组件加载本地H5文件跨域
211浏览 • 1回复 待解决
HarmonyOS web组件上传文件
432浏览 • 1回复 待解决
HarmonyOS 使用Web组件加载页面示例
622浏览 • 1回复 待解决