用户3116712
用户3116712
  • 发布:2026-08-04 10:57
  • 更新:2026-08-04 10:57
  • 阅读:10

Navigation在分栏模式下,如何给右侧空白部分设置默认页面

分类:nvue

问题现象
Navigation在Split模式下,未点击左侧导航栏时,右侧子页显示区显示为空白。当未进行页面路由推送时,如何给右侧的空白部分设置默认展示页面?

背景知识
Navigation组件的分栏模式由mode属性控制,包括单栏(Stack)、分栏(Split)和自适应(Auto)三个属性。该属性默认为Auto模式,在该模式下会自动监听屏幕属性,当为折叠屏或平板时,默认分栏显示,在折叠状态或普通手机时可为单栏显示。
splitPlaceholder:在API20下,Navigation双栏模式支持设置右侧页面显示默认占位页,占位页仅作为UI展示页,不可获焦和响应事件。
解决方案
方案一:push方法推送默认页面。
编写默认页面,并重写onBackPressed返回,当在该页面返回时直接退出软件。
Navigation主页执行aboutToAppear,将默认页面加入页面栈进行默认显示。
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;
});
}
}
实现效果如下:

点击放大

方案二:采用API20中的splitPlaceholder属性。
详情参考:示例14。

常见FAQ
Q:API20以下,当采用Auto模式时,如何判断是否需要推送默认显示页面?

A:可以通过以下方式在aboutToAppear中判断是否需要推送默认页面:

通过设备信息@ohos.deviceInfo接口获取设备的deviceType类型。
通过屏幕属性@ohos.display的isFoldable方法返回当前设备是否可折叠的结果。
总结
实现默认展示页面的方式为,创建一个默认页面,在创建主页时自动推送该默认页面即可,其他关联实现方式参照常见FAQ实现即可。
https://coub.com/view/4b7lh7
https://coub.com/view/4b7lh5
https://coub.com/view/4b7lh4
https://coub.com/view/4b7lh1
https://coub.com/view/4b7lh0
https://coub.com/view/4b7lgx
https://coub.com/view/4b7lgu
https://coub.com/view/4b7lgr
https://coub.com/view/4b7lgo
https://coub.com/view/4b7lgj
https://coub.com/view/4b7lgh
https://coub.com/view/4b7lgd
https://coub.com/view/4b7lgb
https://coub.com/view/4b7lg6
https://coub.com/view/4b7lg2
https://coub.com/view/4b7lfw
https://coub.com/view/4b7lft
https://coub.com/view/4b7lfn
https://coub.com/view/4b7lfh
https://coub.com/view/4b7lfa
https://coub.com/view/4b7lek
https://coub.com/view/4b7leh
https://coub.com/view/4b7lef
https://coub.com/view/4b7lec
https://coub.com/view/4b7leb
https://coub.com/view/4b7le7
https://coub.com/view/4b7le1
https://coub.com/view/4b7ldz
https://coub.com/view/4b7ldx
https://coub.com/view/4b7ldw
https://coub.com/view/4b7ldt
https://coub.com/view/4b7ldq
https://coub.com/view/4b7ldn
https://coub.com/view/4b7lda
https://coub.com/view/4b7ld8
https://coub.com/view/4b7ld4
https://coub.com/view/4b7lcz
https://coub.com/view/4b7lcv
https://coub.com/view/4b7lcq
https://coub.com/view/4b7lco
https://coub.com/view/4b7lcm
https://coub.com/view/4b7lcj
https://coub.com/view/4b7lcd
https://coub.com/view/4b7lc9
https://coub.com/view/4b7lc0
https://coub.com/view/4b7lbx
https://coub.com/view/4b7lbw
https://coub.com/view/4b7lbv
https://coub.com/view/4b7lbu
https://coub.com/view/4b7lbt
https://coub.com/view/4b7lbc
https://coub.com/view/4b7lb5
https://coub.com/view/4b7lax
https://coub.com/view/4b7laj
https://coub.com/view/4b7laf
https://coub.com/view/4b7lae
https://coub.com/view/4b7lab
https://coub.com/view/4b7la7
https://coub.com/view/4b7l9u
https://coub.com/view/4b7l9s
https://coub.com/view/4b7l9q
https://coub.com/view/4b7l9n
https://coub.com/view/4b7l9k
https://coub.com/view/4b7l9j
https://coub.com/view/4b7l9i
https://coub.com/view/4b7l9h
https://coub.com/view/4b7l9g
https://coub.com/view/4b7l9f
https://coub.com/view/4b7l99
https://coub.com/view/4b7l95
https://coub.com/view/4b7l93
https://coub.com/view/4b7l90
https://coub.com/view/4b7l8u
https://coub.com/view/4b7l8r
https://coub.com/view/4b7l8q
https://coub.com/view/4b7l8p
https://coub.com/view/4b7l8o
https://coub.com/view/4b7l8m
https://coub.com/view/4b7l8k
https://coub.com/view/4b7l8g
https://coub.com/view/4b7l8f
https://coub.com/view/4b7l8e
https://coub.com/view/4b7l8b
https://coub.com/view/4b7l88
https://coub.com/view/4b7l86
https://coub.com/view/4b7l84
https://coub.com/view/4b7l81
https://coub.com/view/4b7l7z
https://coub.com/view/4b7l7x
https://coub.com/view/4b7l7w
https://coub.com/view/4b7l7v
https://coub.com/view/4b7l7u
https://coub.com/view/4b7l7s

0 关注 分享

要回复文章请先登录注册