HarmonyOS 鸿蒙Next:WEB加载本地vue项目出现白屏,按照官网的【解决Web组件本地资源跨域问题】案例操作了还是不行
HarmonyOS 鸿蒙Next:WEB加载本地vue项目出现白屏,按照官网的【解决Web组件本地资源跨域问题】案例操作了还是不行
WEB加载本地vue项目出现白屏,按照官网的【解决Web组件本地资源跨域问题 】案例操作了还是不行
import { webview } from '@kit.ArkWeb';
import web_webview from '@ohos.web.webview'
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
// [@State](/user/State) message: string = 'Hello World';
controller: webview.WebviewController = new webview.WebviewController();
webviewController: web_webview.WebviewController = new web_webview.WebviewController();
// 构造域名和本地文件的映射表
schemeMap = new Map([
["https://www.example.com/fullPayment/index.html", "fullPayment/index.html"],
["https://www.example.com/fullPayment/assets/index-a78543f1.js", "fullPayment/assets/index-3035a6f2.js"],
])
// 构造本地文件和构造返回的格式mimeType
mimeTypeMap = new Map([
["fullPayment/index.html", 'text/html'],
["fullPayment/assets/index-3035a6f2.js", "text/javascript"]
])
[@State](/user/State) currentIndex: number = 0
[@Builder](/user/Builder) tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
Column() {
Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
.size({ width: 20, height: 20 })
.margin({bottom:6})
Text(title)
.fontSize(12)
.fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
}
.width('100%')
.height(66)
.justifyContent(FlexAlign.Center)
}
build() {
Tabs({barPosition:BarPosition.End}) {
TabContent() {
}
.backgroundColor('#eeeeee')
.tabBar(this.tabBuilder('首页', 0, $r('app.media.home_selected'), $r('app.media.home_normal')))
.padding({ left: 20, right: 20 })
TabContent() {
Web({ src: 'https://www.baidu.com', controller: this.controller })
.javaScriptAccess(true)
.domStorageAccess(true)
}
.backgroundColor('#eeeeee')
.tabBar(this.tabBuilder('财富', 0, $r('app.media.home_selected'), $r('app.media.home_normal')))
.padding({ left: 20, right: 20 })
TabContent() {
Web({ src: $rawfile('index.html'), controller: this.controller })
.javaScriptAccess(true)
.domStorageAccess(true)
}
.backgroundColor('#eeeeee')
.tabBar(this.tabBuilder('生活', 0, $r('app.media.home_selected'), $r('app.media.home_normal')))
.padding({ left: 20, right: 20 })
TabContent() {
// Web({ src: $rawfile('mine/index.html'), controller: this.controller })
// .javaScriptAccess(true)
// .domStorageAccess(true)
// 针对本地index.html,使用http或者https协议代替file协议或者resource协议,并且构造一个属于自己的域名。
// 本例中构造www.example.com为例。
Web({ src: "https://www.example.com/fullPayment/index.html", controller: this.webviewController })
.javaScriptAccess(true)
.fileAccess(true)
.domStorageAccess(true)
.geolocationAccess(true)
.width("100%")
.height("100%")
.onInterceptRequest((event) => {
if (!event) {
return;
}
// 此处匹配自己想要加载的本地离线资源,进行资源拦截替换,绕过跨域
if (this.schemeMap.has(event.request.getRequestUrl())) {
let rawfileName: string = this.schemeMap.get(event.request.getRequestUrl())!;
console.log("Access to script at11 "+event.request.getRequestUrl());
console.log("Access to script at22 "+rawfileName);
// console.log(event.request.getRequestUrl());
let mimeType = this.mimeTypeMap.get(rawfileName);
console.log("Access to script at33 "+mimeType);
if (typeof mimeType === 'string') {
let response = new WebResourceResponse();
// 构造响应数据,如果本地文件在rawfile下,可以通过如下方式设置
console.log("Access to script at44 "+ $rawfile(rawfileName));
response.setResponseData($rawfile(rawfileName));
response.setResponseEncoding('utf-8');
response.setResponseMimeType(mimeType);
response.setResponseCode(200);
response.setReasonMessage('OK');
response.setResponseIsReady(true);
return response;
}
}
return null;
})
}
.backgroundColor('#eeeeee')
.tabBar(this.tabBuilder('我的', 0, $r('app.media.home_selected'), $r('app.media.home_normal')))
.padding({ left: 20, right: 20 })
// Column() {
// }
}
.height('100%')
.width('100%')
}
}
Your Code
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
哪位大神能给指点一下?
2 回复
这个问题我们解决了,我们静态资源很多,需要将所有资源都加上映射才能正确显示。所以我直接用如下方式:
//去掉映射代码Web({ src: "https://www.example.com/fullPayment/index.html", controller: this.controller })
.javaScriptAccess(true)
.domStorageAccess(true)
.javaScriptAccess(true)
.fileAccess(true)
.domStorageAccess(true)
.geolocationAccess(true)
.width("100%")
.height("100%")
.onInterceptRequest((event) => {
if (!event) {
return;
}
//直接截取路径这样对于需要映射很多静态文件就方便多了。
let rawfileName: string = event.request.getRequestUrl().substring(24); //24个字符截取的是域名https://www.example.com
let mimeType = '';
if (rawfileName.endsWith('html')) {
mimeType = 'text/html'
} else if (rawfileName.endsWith('css')) {
mimeType = 'text/css'
} else if (rawfileName.endsWith('js')) {
mimeType = 'text/javascript'
} else if (rawfileName.endsWith('png')) {
mimeType = 'image/png'
} else if (rawfileName.endsWith('jpg')) {
mimeType = 'image/jpeg'
}
if (typeof mimeType === 'string') {
let response = new WebResourceResponse();
response.setResponseData($rawfile(rawfileName));
response.setResponseEncoding('utf-8');
response.setResponseMimeType(mimeType);
response.setResponseCode(200);
response.setReasonMessage('OK');
response.setResponseIsReady(true);
return response;
}
// }
return null;
})
.height('100%')
.width('100%')
针对HarmonyOS鸿蒙系统中WEB加载本地Vue项目出现白屏的问题,首先确认是否已正确配置本地服务器以支持跨域请求。此外,检查Vue项目的构建配置,确保生产环境的资源路径(如JS、CSS等)正确无误,且没有因路径问题导致资源加载失败。同时,查看浏览器控制台是否有错误信息,这有助于定位问题。如果以上步骤均无误,尝试清除浏览器缓存或重启设备。如果问题依旧没法解决请加我微信,我的微信是itying888。