通过onLoadIntercept拦截请求,识别自定义协议链接并跳转其它页面,从其它页面返回到Web页,Web页面是白屏。
import { webview } from '@kit.ArkWeb';
import { TargetPage } from './TargetPage';
@Entry
@Component
struct Index {
private webController: webview.WebviewController = new webview.WebviewController();
private webUrl: ResourceStr = $rawfile('index.html');
@Provide('pageStack') pageStack: NavPathStack = new NavPathStack();
@Builder
pageMap(name: string) {
if (name === 'TargetPage') {
TargetPage();
}
}
build() {
Navigation(this.pageStack) {
Column() {
Web({
controller: this.webController,
src: this.webUrl
})
.onLoadIntercept((event) => {
if (!event) {
return false;
}
const url = event.data.getRequestUrl();
// 自定义协议跳转HarmonyOS页面
if (url.startsWith('detail:xxxx')) {
this.pageStack.pushPathByName('TargetPage', null);
}
return false;
})
.fileAccess(false)
.geolocationAccess(false)
}
}.navDestination(this.pageMap)
// 隐藏标题栏
.hideTitleBar(true);
}
} 在Web组件的onLoadIntercept回调中拦截url并执行相应行为后,如果不需要加载该url应立即return true阻止该url访问。
import { webview } from '@kit.ArkWeb';
import { TargetPage } from './TargetPage';
@Entry
@Component
struct Index {
@Provide('pageStack') pageStack: NavPathStack = new NavPathStack();
private webController: webview.WebviewController = new webview.WebviewController();
private webUrl: ResourceStr = $rawfile('index.html');
@Builder
pageMap(name: string) {
if (name === 'TargetPage') {
TargetPage();
}
}
build() {
Navigation(this.pageStack) {
Column() {
Web({
controller: this.webController,
src: this.webUrl
})
.onLoadIntercept((event) => {
if (!event) {
return false;
}
const url = event.data.getRequestUrl();
// 自定义协议跳转HarmonyOS页面
if (url.startsWith('detail:xxxx')) {
this.pageStack.pushPathByName('TargetPage', null); // 跳转目标页面
return true;
}
return false;
})
.fileAccess(false)
.geolocationAccess(false);
};
}.navDestination(this.pageMap)
// 隐藏标题栏
.hideTitleBar(true);
}
} HTML页面如下:
<!-- 文件路径:src/main/resources/rawfile/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HarmonyOS Web Demo</title>
<style>
a {
display: inline-block;
padding: 10px 20px;
background-color: #007dff;
color: white;
text-decoration: none;
border-radius: 5px;
margin-top: 20px;
}
a:active {
background-color: #005bb5;
}
</style>
</head>
<body>
<h1>Web组件示例</h1>
<p>点击下方链接触发路由跳转:</p>
<!-- 自定义协议跳转链接 -->
<a href="detail:xxxx">点击跳转HarmonyOS原生页面</a>
</body>
</html> 跳转页面TargetPage(用于验证跳转后的返回功能)如下:
// 跳转的目标页面
@Component
export struct TargetPage {
build() {
NavDestination() {
Column() {
Text('TargetPage');
};
};
}
}