用户3111234
用户3111234
  • 发布:2026-07-20 16:34
  • 更新:2026-07-20 16:34
  • 阅读:90

如何解决NavDestination页面做首页时跳转动画异常问题

分类:uni-app

问题现象
由于Navigation导航页的功能限制,用导航页作为首页时,无法将首页推送入栈,只能通过清空路由栈的方式返回首页,在一些场景下不适用。所以开发时可能会选择首页不实现UI布局,采用子页代替首页的实现方式。参考代码如下:

导航页空白布局。
@Entry
@Component
struct NavigationExample {
pageInfos: NavPathStack = new NavPathStack();

aboutToAppear() {
this.pageInfos.pushPath({ name: 'pageOne' }); // 推送第一个子页作为首页
}

build() {
Navigation(this.pageInfos) {
}
.backgroundColor(Color.Gray); // 为了使动画异常更明显设置对比色
}
}
第一个子页代替导航页成为首页。
@Builder
export function PageOneBuilder(name: string, param: Object) {
PageOne();
}

@Component
export struct PageOne {
pageInfos: NavPathStack = new NavPathStack();
@State getAllPathName: string[] = [];

build() {
NavDestination() {
Column() {
Button('我为首页', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20);
}
.width('100%')
.height('100%');
}
.title('pageOne')
.onReady((context: NavDestinationContext) => {
this.pageInfos = context.pathStack;
});
}
}
上述实现方式,在打开应用时导航页总是会显示一瞬间,如何实现子页代替导航页成为首页的功能,并避免这种跳转异常?

点击放大

背景知识
Navigation是路由导航组件,可以采用pushPath、pushPathByName等方式进行路由跳转。这些跳转方式默认集成了动画的功能,同时也可以通过设置关闭跳转时的动画功能。

问题定位
导航页跳转子页操作是在aboutToAppear内执行,程序首先加载导航页,且跳转默认具有动画效果,所以有空白主页左移和子页出现的过程。

分析结论
未关闭跳转的动画效果,所以在UI显示上有导航页显示的情况。

修改建议
方案一:采用hideNavBar隐藏导航页。
隐藏导航页时会取消动画,主页代码修改如下,PageOne页面代码同问题现象的子页。

@Entry
@Component
struct HideNavBarPage {
pageInfos: NavPathStack = new NavPathStack();

aboutToAppear() {
this.pageInfos.pushPath({ name: 'pageOne' }); // 推送第一个子页作为首页
}

build() {
Navigation(this.pageInfos) {
}
.backgroundColor(Color.Gray)
.hideNavBar(true); // 隐藏导航页
}
}
方案二:修改pushPath等入栈方法属性。
pushPath入参animated参数默认为true支持动画,可以设置为false。
pushPath入参NavPathInfo可以设置isEntry属性,该参数表示是否作为首页推送子页,当该参数设置为true时,从该页面按返回键会自动返回桌面。
修改方式如下:

把子页作为主页推送。
@Entry
@Component
struct NavigationExample {
pageInfos: NavPathStack = new NavPathStack();

aboutToAppear() {
this.pageInfos.pushPath({ name: 'pageOne', isEntry: true }, false); // 作为入口页面推送,并关闭动画
}

build() {
Navigation(this.pageInfos) {
}
.backgroundColor(Color.Gray);
}
}
子页重写返回逻辑。若不重写返回,在未结束app进程的前提下,isEntry参数只能支持一次返回桌面的逻辑。
.onBackPressed(() => {
try {
// 获取Context
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
// 通过Context获取windowStage
let windowStage = context.windowStage;
// 通过windowStage获取主窗口
let mainWindow = windowStage.getMainWindowSync();
// 将主窗口最小化
mainWindow.minimize();
} catch (exception) {
console.error(onBackPress failed. Cause code: ${exception.code}, message: ${exception.message});
}
return true;
});
说明
请参考系统路由表的配置方法,两种方案运行还需在模块的module.json5文件中添加路由表配置"routerMap": "$profile:route_map",并在工程resources/base/profile中创建route_map.json文件。添加如下配置信息:

{
"routerMap": [
{
"name": "pageOne",
"pageSourceFile": "src/main/ets/pages/pageOne.ets",
"buildFunction": "PageOneBuilder"
}
]
}
上述两种方案,不可使用clear方式清空路由栈,即使清空后也必须立即推送主页。

https://pastebin.com/JAVQmp79
https://pastebin.com/k9smzpTH
https://pastebin.com/rDcsavhR
https://pastebin.com/cqMHpXeL
https://pastebin.com/BR1sS49t
https://pastebin.com/3bWumYSX
https://pastebin.com/eGHppucy
https://pastebin.com/CBX31Giw
https://pastebin.com/8kmk874f
https://pastebin.com/CKJMYUYA
https://pastebin.com/eNdzC5sZ
https://pastebin.com/k3rGKmss
https://pastebin.com/DXrHJn0Q
https://pastebin.com/6dJyCNcW
https://pastebin.com/3QWNvD53
https://pastebin.com/KHw2gdyK
https://pastebin.com/zZqzbgpn
https://pastebin.com/h5S00Q5T
https://pastebin.com/wM9hUtKV
https://pastebin.com/z1TWT48E
https://pastebin.com/zsY6cGRn
https://pastebin.com/9tcsi4Cd
https://pastebin.com/Gy5a55jb
https://pastebin.com/WDVLDinU
https://pastebin.com/x5HQvgn9
https://pastebin.com/6EvyrpQw
https://pastebin.com/e9UgA5vg
https://pastebin.com/ahJgtFnL
https://pastebin.com/Fyi8EHqb
https://pastebin.com/3ZAJdsUX
https://pastebin.com/xA7WLU3H
https://pastebin.com/bWs0Ee8f
https://pastebin.com/R3fhkaq3
https://pastebin.com/uLSsS9sp
https://pastebin.com/Y1b4M3n2
https://pastebin.com/w8P3z7QA
https://pastebin.com/0YfzYjDq
https://pastebin.com/iiX2Kj9m
https://pastebin.com/rFbnzNys
https://pastebin.com/KfbvZpTu
https://pastebin.com/MJdECHXV
https://pastebin.com/LEwpHRmY
https://pastebin.com/WAEjH7wy
https://pastebin.com/mKqk52gR
https://pastebin.com/SiVt6Qq8
https://pastebin.com/L3WkXkAY
https://pastebin.com/j18zLzsF
https://pastebin.com/78UHPRZ9
https://pastebin.com/Ae4xdiYL
https://pastebin.com/b0kDPSsG
https://pastebin.com/xwwSFYZL
https://pastebin.com/dEAL4c8A
https://pastebin.com/8zZti8Sq
https://pastebin.com/nvJeQ2uX
https://pastebin.com/yJCPjtyV
https://pastebin.com/hRYa30hk
https://pastebin.com/pkahv2CK
https://pastebin.com/cfGZXgeK
https://pastebin.com/iYYgWKyB
https://pastebin.com/GvNxpv9D
https://pastebin.com/pfH8jmJe
https://pastebin.com/Kb0BhLgq
https://pastebin.com/4yJUxidQ
https://pastebin.com/C3FjuTuz
https://pastebin.com/A8DVZwGq
https://pastebin.com/eDRz720M
https://pastebin.com/hi4DGeGR
https://pastebin.com/iLyjgd2s
https://pastebin.com/vjr1i9AD
https://pastebin.com/ktjj2MRv
https://pastebin.com/hudR6nWx
https://pastebin.com/2eFc2G6w
https://pastebin.com/uYPjQZ5U
https://pastebin.com/jiyHh5QZ
https://pastebin.com/zwqXswXm
https://pastebin.com/z72myVp0
https://pastebin.com/yaKShW2V
https://pastebin.com/WpduHmwe
https://pastebin.com/mzHesPES
https://pastebin.com/4Ag3NVDA
https://pastebin.com/FtwpiLWb
https://pastebin.com/gYNVscWW
https://pastebin.com/BR9kvRL6
https://pastebin.com/XgiArnSf
https://pastebin.com/FrA1tL8K
https://pastebin.com/DsYHRgcK
https://pastebin.com/yQjRG7MV
https://pastebin.com/qMWTx6DS
https://pastebin.com/40Vk0SPz
https://pastebin.com/LUAWy67H
https://pastebin.com/Z8yyb3E6
https://pastebin.com/QFkmY1jt
https://pastebin.com/qHT7Pu7A
https://pastebin.com/32uRiUYa
https://pastebin.com/p9MAhN6B
https://pastebin.com/zyuctqGg
https://pastebin.com/95vYj138
https://pastebin.com/3AzCXRxN
https://pastebin.com/Q11kTuYN
https://pastebin.com/cbzLRT0x
https://pastebin.com/gC9GJXkX
https://pastebin.com/kM66aNqc
https://pastebin.com/VEBReGjM
https://pastebin.com/NNsKd0MJ
https://pastebin.com/9X2T5uZG
https://pastebin.com/dGu1qcZz
https://pastebin.com/dYFGCUHF
https://pastebin.com/9ssHQnyz
https://pastebin.com/qJnfNRrs
https://pastebin.com/Hgz8jrd1
https://pastebin.com/HYWQKvKY
https://pastebin.com/CpW2rvGm
https://pastebin.com/w3Y3py7m
https://pastebin.com/N7tdhANv
https://pastebin.com/dJpTKWgv
https://pastebin.com/fuXWx5tT
https://pastebin.com/9fJAkwQm
https://pastebin.com/hFZ2KxmE
https://pastebin.com/ze6HArpr
https://pastebin.com/yZm2w4tv
https://pastebin.com/Npt44XF6
https://pastebin.com/fs4W1vyG
https://pastebin.com/GXBsyD1Z
https://pastebin.com/BVT8YNCd
https://pastebin.com/gpyEYEdp
https://pastebin.com/Pg1Az3B2
https://pastebin.com/qnSMDBxM
https://pastebin.com/bFN5qbGz
https://pastebin.com/35uhMsNM
https://pastebin.com/tJkzQKLX
https://pastebin.com/fZwbWev8
https://pastebin.com/4aRyndYy
https://pastebin.com/zk9u2XBN
https://pastebin.com/FqzQumiD
https://pastebin.com/qQDjkByy
https://pastebin.com/eWgr65Z3
https://pastebin.com/bAuTceJ0
https://pastebin.com/RvDHcvJs
https://pastebin.com/cUfMPrAF
https://pastebin.com/pv89yV8j
https://pastebin.com/EZJzTG7k
https://pastebin.com/LMjT0F6B
https://pastebin.com/Ym2ddLsR
https://pastebin.com/1qwHrQaC
https://pastebin.com/qCpSeQHE
https://pastebin.com/Jfia1spt
https://pastebin.com/QXqkPSjj
https://pastebin.com/EK7zX9js
https://pastebin.com/3cXDqFp6
https://pastebin.com/hkrCr9WX
https://pastebin.com/VMizncXN
https://pastebin.com/b7J7V6Qa
https://pastebin.com/CY5kPJDU
https://pastebin.com/TZnQEcpM
https://pastebin.com/f5hhvZQT
https://pastebin.com/1iJAL6gE
https://pastebin.com/7RKAAzmY
https://pastebin.com/0cvfH3Rj
https://pastebin.com/jWZbySm0
https://pastebin.com/ihX0S3YH
https://pastebin.com/DtcCFiwY
https://pastebin.com/U0EkB7Pr
https://pastebin.com/Au4yJuei
https://pastebin.com/S1RtjTKK
https://pastebin.com/NMk5VG8r
https://pastebin.com/sB6bf2ys
https://pastebin.com/MPc6QFtk
https://pastebin.com/M5Wr62Qe
https://pastebin.com/Jhu7dsjX
https://pastebin.com/wHzpZiD3
https://pastebin.com/7R38JNeY
https://pastebin.com/x1ipxp4Y
https://pastebin.com/Yk2jfYLA
https://pastebin.com/eXxx43Qb
https://pastebin.com/XQPDTixC
https://pastebin.com/Swqpn5R1
https://pastebin.com/6FiQTkSk
https://pastebin.com/RQ4Bsg0D
https://pastebin.com/ZPzXF3r2
https://pastebin.com/nJ4bCp1W
https://pastebin.com/HP9LGLeU
https://pastebin.com/spL57eMv
https://pastebin.com/DvvCxfbt
https://pastebin.com/6P7Mh8iQ
https://pastebin.com/TaRPbrA7
https://pastebin.com/i1LCQG91
https://pastebin.com/6WJm5C1d
https://pastebin.com/aNETPLnH
https://pastebin.com/50WrUuJ8
https://pastebin.com/93wNYfPi
https://pastebin.com/UYBvmRXh
https://pastebin.com/GF2YHtF1
https://pastebin.com/U0YF8sFc
https://pastebin.com/qT39ibbH
https://pastebin.com/JBXn8XRy
https://pastebin.com/4du9GZBZ
https://pastebin.com/XT0DSL0K
https://pastebin.com/M03sDpmj
https://pastebin.com/6Um4QBUQ
https://pastebin.com/jaFyU43B
https://pastebin.com/aXFN68xW
https://pastebin.com/VxG6G4jg
https://pastebin.com/WcwSxAE0
https://pastebin.com/GfJzGF1n
https://pastebin.com/4jFH8PVX
https://pastebin.com/Efn8cfbB
https://pastebin.com/3TGPNnKb
https://pastebin.com/RVcaZcf1
https://pastebin.com/iZED19hM
https://pastebin.com/rJBzPkkq
https://pastebin.com/td2crfd5
https://pastebin.com/6wrykVHe
https://pastebin.com/fkD1gUU7
https://pastebin.com/sAEYa89u
https://pastebin.com/qdbeXNbr
https://pastebin.com/1j8UeXhw
https://pastebin.com/eNSpj8Ju
https://pastebin.com/kEeY5C2C
https://pastebin.com/t7Ltzh5Y
https://pastebin.com/7f0Am9c5
https://pastebin.com/HFLUhJKh
https://pastebin.com/MpM96WSp
https://pastebin.com/q2ijVQTG
https://pastebin.com/weVGSM8L
https://pastebin.com/pftK0PYe
https://pastebin.com/14aW4aR5
https://pastebin.com/h1XwmePB
https://pastebin.com/8Qi0Pw6u
https://pastebin.com/TUjN7fDM
https://pastebin.com/3w8XfQgt
https://pastebin.com/hVx03mJc
https://pastebin.com/Nh3Bzv2e
https://pastebin.com/t7bHQwLH
https://pastebin.com/9h0kpJZ9
https://pastebin.com/N6HMDWjT
https://pastebin.com/cEMGWYfx
https://pastebin.com/5bvqQ9uN
https://pastebin.com/pnsVdSZu
https://pastebin.com/tASbU52N
https://pastebin.com/c9YZep8B
https://pastebin.com/wd9zNpGg
https://pastebin.com/0EzGYBsW
https://pastebin.com/J9eLX33b
https://pastebin.com/q4qaYDwr
https://pastebin.com/MDPd7Nk4
https://pastebin.com/vkiFQBZj
https://pastebin.com/re6PhK4e
https://pastebin.com/2AukKinc
https://pastebin.com/cJCiFTgV
https://pastebin.com/WwKdrppQ
https://pastebin.com/gQb4TGNt
https://pastebin.com/uYadFLcE
https://pastebin.com/TYrNgG7M
https://pastebin.com/jwL9ap2s
https://pastebin.com/VAh0iHsS
https://pastebin.com/CjR4fH8r
https://pastebin.com/5swgD7Xh
https://pastebin.com/PwKEebz0
https://pastebin.com/aATHDEg7
https://pastebin.com/E6x8tEMK
https://pastebin.com/TBzuA51e
https://pastebin.com/Pg77Rigg
https://pastebin.com/3wGpSpVD
https://pastebin.com/Hfd335RM
https://pastebin.com/VZ6wBMGr
https://pastebin.com/hni7Lgqd
https://pastebin.com/9qXDVKhH
https://pastebin.com/3Mqzfw9f
https://pastebin.com/DYnqGd8s
https://pastebin.com/htdThyTD
https://pastebin.com/jn2H23Rm
https://pastebin.com/fwxGyvw9
https://pastebin.com/TSKG2Nrg
https://pastebin.com/chYiVyrr
https://pastebin.com/HMSt1MCp
https://pastebin.com/dgyXyzei
https://pastebin.com/rZmBGVRb
https://pastebin.com/Tqirtkc2
https://pastebin.com/nsDsZnNj
https://pastebin.com/Am46yTdP
https://pastebin.com/imhV55Yy
https://pastebin.com/JnDPTjfM
https://pastebin.com/L6Ui0s8c
https://pastebin.com/ugFVj1fh
https://pastebin.com/CyHkREK0
https://pastebin.com/zMeTawYF
https://pastebin.com/fYiXDzYX
https://pastebin.com/dPhMM9Jr
https://pastebin.com/W5kshesT
https://pastebin.com/XzZxruVF
https://pastebin.com/mtxqJQxr
https://pastebin.com/KdnyJWM9
https://pastebin.com/HTWs1VEn
https://pastebin.com/azXgwaJv
https://pastebin.com/7fDZUbxP
https://pastebin.com/Rt8FWgHz
https://pastebin.com/jDCWBSRm
https://pastebin.com/DxrRdXdG
https://pastebin.com/eDA1BrFv
https://pastebin.com/Th9FhFgk

2026-07-20 16:34 负责人:无 分享
已邀请:

要回复问题请先登录注册