import { window } from '@kit.ArkUI';
@Entry
@Component
struct NavigationExample {
@Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack();
private arr: number[] = [1, 2, 3];
aboutToAppear(): void {
this.pageInfos.pushPath({ name: 'DefaultPage' });
}
@Builder
PageMap(name: string) {
if (name === 'NavDestinationTitle1') {
pageOneTmp();
} else if (name === 'NavDestinationTitle2') {
pageTwoTmp();
} else if (name === 'NavDestinationTitle3') {
pageThreeTmp();
} else if (name === 'DefaultPage') {
DefaultPage();
}
}
build() {
Column() {
Navigation(this.pageInfos) {
TextInput({ placeholder: 'search...' })
.width('90%')
.height(40)
.backgroundColor('#FFFFFF');
List({ space: 12 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('Page' + item)
.width('100%')
.height(72)
.backgroundColor('#FFFFFF')
.borderRadius(24)
.fontSize(16)
.fontWeight(500)
.textAlign(TextAlign.Center)
.onClick(() => {
this.pageInfos.pushPath({ name: 'NavDestinationTitle' + item });
});
};
}, (item: number) => item.toString());
}
.width('90%')
.margin({ top: 12 });
}
.title('主标题')
.mode(NavigationMode.Split)
.navDestination(this.PageMap);
}
.height('100%')
.width('100%')
.background('#F1F3F5');
}
}
// PageOne页面
@Component
export struct pageOneTmp {
@Consume('pageInfos') pageInfos: NavPathStack;
build() {
NavDestination() {
Column() {
Text('NavDestinationContent1');
}.width('100%').height('100%').justifyContent(FlexAlign.Center);
}.title('NavDestinationTitle1');
}
}
// PageTwo页面
@Component
export struct pageTwoTmp {
@Consume('pageInfos') pageInfos: NavPathStack;
build() {
NavDestination() {
Column() {
Text('NavDestinationContent2');
}.width('100%').height('100%').justifyContent(FlexAlign.Center);
}.title('NavDestinationTitle2');
}
}
// PageThree页面
@Component
export struct pageThreeTmp {
@Consume('pageInfos') pageInfos: NavPathStack;
build() {
NavDestination() {
Column() {
Text('NavDestinationContent3');
}.width('100%').height('100%').justifyContent(FlexAlign.Center);
}.title('NavDestinationTitle3');
}
}
@Component
export struct DefaultPage {
@Consume('pageInfos') pageInfos: NavPathStack;
build() {
NavDestination() {
Column() {
Text('DefaultPage');
}.width('100%').height('100%').justifyContent(FlexAlign.Center);
}.title('DefaultPage')
.onBackPressed(() => {
// 返回true表示自定义返回,能避免返回空白页面。返回false则表示系统默认返回,会返回空白页面。
try {
// 需要导入window
window.getLastWindow(this.getUIContext().getHostContext(), (err, win) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
win.minimize((err) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to minimize the window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
console.info('Succeeded in minimizing the window.');
});
});
} catch (exception) {
console.error(`Failed to obtain the top window. Cause code: ${exception.code}, message: ${exception.message}`);
}
return true;
});
}
} 实现效果如下:

详情参考:示例14。
Q:API20以下,当采用Auto模式时,如何判断是否需要推送默认显示页面?
A:可以通过以下方式在aboutToAppear中判断是否需要推送默认页面: