解决方案 | 对比 |
|---|---|
方案一:通过隐藏导航页和路由拦截实现不返回开屏页。 | 平板分栏模式下左侧闪屏页、右侧空白。 |
方案二:通过销毁作为开屏页的NavDestination页面,实现不返回开屏页。 | 平板分栏模式下左侧主页、右侧闪屏页。 |
方案三:使用Router页面做闪屏页,主页及后续页面由Navigation路由承载,通过clear清除闪屏页。 | 不受平板分栏的影响。 |
@Entry
@Component
struct InterceptionSolution {
pathStack: NavPathStack = new NavPathStack();
@State hideNavBar: boolean = false;
aboutToAppear(): void {
setTimeout(() => {
this.pathStack.pushPathByName('MainPage', undefined, false);
this.hideNavBar = true;
// 添加路由拦截功能
this.pathStack.setInterception({
willShow: (from: NavDestinationContext | NavBar, to: NavDestinationContext | NavBar,
operation: NavigationOperation, isAnimated: boolean) => {
// 如果返回到首页,就跳转为MainPage页面
if (to == 'navBar') {
console.info(`${from} ${to} ${operation} ${isAnimated}`);
this.getUIContext().getPromptAction().showToast({ message: '无法返回到首页' });
this.pathStack.pushPathByName('MainPage', undefined, false);
}
}
});
}, 2000);
}
@Builder
pagesMap(name: string) {
if (name == 'MainPage') {
MainPage();
} else if (name == 'Page') {
Page();
}
}
build() {
Navigation(this.pathStack) {
Text('欢迎进入APP').fontSize(30);
}.title('启动页')
.hideNavBar(this.hideNavBar)
.navDestination(this.pagesMap);
}
}
@Component
struct MainPage {
pageName: string = '';
pathStack: NavPathStack | undefined = undefined;
build() {
NavDestination() {
Column() {
Text('this is MainPage').fontSize(24);
Button(`push Page`).width('80%').margin({ top: 10, bottom: 10 })
.onClick(() => {
this.pathStack?.pushPathByName('Page', '');
});
Button(`pop`).width('80%').margin({ top: 10, bottom: 10 })
.onClick(() => {
this.pathStack?.pop();
});
};
}.title('MainPage')
.onReady((context: NavDestinationContext) => {
this.pageName = context.pathInfo.name;
this.pathStack = context.pathStack;
});
}
}
@Component
struct Page {
pageName: string = '';
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column() {
Text('this is Page').fontSize(24);
Button(`pop`).width('80%').margin({ top: 10, bottom: 10 })
.onClick(() => {
this.pathStack?.pop();
});
};
}.title('Page')
.onReady((context: NavDestinationContext) => {
this.pageName = context.pathInfo.name;
this.pathStack = context.pathStack;
});
}
} @Entry
@Component
struct RemoveSolution {
@Provide('navPathStack') navPathStack: NavPathStack = new NavPathStack();
@Builder
pageMap(name: string) {
if (name === 'Splash') {
Splash();
}
}
aboutToAppear(): void {
this.navPathStack.pushPathByName('Splash', null, false);
}
build() {
Navigation(this.navPathStack) {
Column() {
Text('首页');
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center);
}
.hideToolBar(true)
.navDestination(this.pageMap);
}
}
@Component
export struct Splash {
@Consume('navPathStack') navPathStack: NavPathStack;
aboutToAppear(): void {
setTimeout(() => {
this.navPathStack.pop(false);
}, 2000);
}
build() {
NavDestination() {
Column() {
Text('闪屏页');
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center);
}
.hideTitleBar(true);
}
} @Entry
@Component
struct RouterSolution {
build() {
RelativeContainer() {
Text('闪屏页')
.fontSize($r('app.float.page_text_font_size'))
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
// 确定用户同意后跳转首页
this.getUIContext().getRouter().pushUrl({ url: 'pages/NavigationPage' });
});
}
.height('100%')
.width('100%');
}
} @Entry
@Component
struct NavigationPage {
private stack: NavPathStack = new NavPathStack();
onPageShow(): void {
// 清除闪屏页,防止返回上一页
this.getUIContext().getRouter().clear();
}
build() {
// 在这里配置主页NavDestination信息
Navigation(this.stack) {
Column() {
Text('Navigation首页,后续使用Navigation实现路由');
}.width('100%').height('100%')
.justifyContent(FlexAlign.Center);
}
.width('100%').height('100%');
}
}