问题现象
使用Navigation构建路由,从pageOne通过pushPath跳转到pageTwo,期望pageOne的onPop回调在pageTwo返回时被触发,但效果未达预期。
问题代码示例参考如下:
class ParamWithOp {
operation: number = 1
count: number = 10
}
@Entry
@Component
struct PageOne {
pageInfo: NavPathStack = new NavPathStack();
@State message: string = 'Hello World'
@Builder
pageMap(name: string, params: Object) {
if (name === 'pageTwo') {
PageTwo()
}
}
build() {
Navigation(this.pageInfo) {
Column() {
Text(this.message)
.width('80%')
.height(50)
.margin(10)
Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(10)
.onClick(() => {
// 将name指定的NavDestination页面信息入栈,传递的数据为param,添加接收处理结果的onPop回调。
this.pageInfo.pushPath({
name: 'pageTwo', param: new ParamWithOp(), onPop: (popInfo: PopInfo) => {
this.message = `[pushPath]last page is: ${popInfo.info.name} result: ${JSON.stringify(popInfo.result)}`
}
});
})
}.width('100%').height('100%')
}.navDestination(this.pageMap)
.title('pageOne')
}
}
@Component
struct PageTwo {
pathStack: NavPathStack = new NavPathStack()
build() {
NavDestination() {
Column() {
Button('pop', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
// 回退到上一个页面,此处代码,在pop回pageOne页面时,未传参数
this.pathStack.pop();
})
}.width('100%').height('100%')
}.title('pageTwo')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack
})
}
}
效果预览
点击放大
背景知识
Navigation组件是路由导航的根视图容器,结合导航控制器NavPathStack可实现组件导航。
pushPath:将info指定的NavDestination页面信息入栈。可设置onPop回调函数来接收参数。
pop:弹出路由栈栈顶元素,并触发onPop回调传入页面处理结果。
问题定位
点击放大
查阅官方文档关于pushPath方法的NavPathInfo入参说明,其中的onPop回调函数仅pop、popToName、popToIndex中设置result参数后触发。
分析结论
onPop回调函数需要使用pop、popToName、popToIndex方法返回时设置result参数才会触发,否则不会执行onPop回调。
修改建议
按上节所述,只需在pageTwo中调用pop方法时,传入result参数,即可在pageOne中成功收到onPop的回调。修改问题代码如下:
// 回退到上一个页面,随便传个result即可触发onPop回调
this.pathStack.pop(1);
修改后的运行效果参见效果预览,可以看到,当pageTwo调用pop返回时传入了result参数,在pageOne成功执行了onPop回调,并接收到相关参数。
https://pastebin.com/NRD7bMyP
https://pastebin.com/yDaZxFwG
https://pastebin.com/EDcLGLPS
https://pastebin.com/U3ZxgwDi
https://pastebin.com/DR63sAxA
https://pastebin.com/nSnDNVPq
https://pastebin.com/GjtSqPNS
https://pastebin.com/saHctNS5
https://pastebin.com/kNHqdQnW
https://pastebin.com/JMnQaXK7
https://pastebin.com/vSexS64Q
https://pastebin.com/tRQUHv5L
https://pastebin.com/3JqBJ8wq
https://pastebin.com/xEkHBut4
https://pastebin.com/skZmkkFw
https://pastebin.com/7V0wmLFb
https://pastebin.com/BTZfE8Bd
https://pastebin.com/svjxZfvM
https://pastebin.com/xXYHPU8b
https://pastebin.com/zMLjuJuV
https://pastebin.com/ffbfvSna
https://pastebin.com/C9Zr1K2M
https://pastebin.com/ac9xpyfm
https://pastebin.com/WX45yC8p
https://pastebin.com/YeUrZij0
https://pastebin.com/zKW8HRQb
https://pastebin.com/6KBWHYZG
https://pastebin.com/MTtnz3t8
https://pastebin.com/E7SBb1wr
https://pastebin.com/eib2hRUq
https://pastebin.com/SEeERuCT
https://pastebin.com/8tLt3B2T
https://pastebin.com/R4NJytjp
https://pastebin.com/EauQE9nS
https://pastebin.com/fsCXAP9r
https://pastebin.com/9vFQE41K
https://pastebin.com/xyKY9DF0
https://pastebin.com/4duLr15i
https://pastebin.com/Epk3dbmH
https://pastebin.com/GehEwq1u
https://pastebin.com/HyQm4h82
https://pastebin.com/D5vZs1MG
https://pastebin.com/L2KryS4C
https://pastebin.com/QBtyQipw
https://pastebin.com/FNxMPZ51
https://pastebin.com/trw5d8cC
https://pastebin.com/rPFzkgK8
https://pastebin.com/uCBsSjNm
https://pastebin.com/RGW6X15L
https://pastebin.com/S7cBiuDb
https://pastebin.com/HFyJtEKK
https://pastebin.com/0ubhVneF
https://pastebin.com/RW5trQzf
https://pastebin.com/vCSGjNpZ
https://pastebin.com/SffvfhGR
https://pastebin.com/tt0tYGFq
https://pastebin.com/tMm7YFVa
https://pastebin.com/ibcT2Lf2
https://pastebin.com/JqjbbVRf
https://pastebin.com/HVKWqHGk
https://pastebin.com/s5vt779b
https://pastebin.com/B0TAyxn6
https://pastebin.com/fuPRhvhd
https://pastebin.com/beLXK35p
https://pastebin.com/GFBwqxu6
https://pastebin.com/9VWb8miy
https://pastebin.com/grHVbPND
https://pastebin.com/CYaQEmTS
https://pastebin.com/ZkmuiH18
https://pastebin.com/f2xJpBQ9
https://pastebin.com/2W5h2q09
https://pastebin.com/rAC3TU64
https://pastebin.com/K9pyh9gi
https://pastebin.com/uz28wUPg
https://pastebin.com/cVAkkb9F
https://pastebin.com/RbNxE2S2
https://pastebin.com/hrxFwi1b
https://pastebin.com/6dxqezH3
https://pastebin.com/SQmcrc6u
https://pastebin.com/DmzbU6WX
https://pastebin.com/RQzvD6gG
https://pastebin.com/nWdqiQr9
https://pastebin.com/qSiWmiq9
https://pastebin.com/HbjBAg6q
https://pastebin.com/7sg5aa6M
https://pastebin.com/cGDzhrRb
https://pastebin.com/Q3D2BmaQ
https://pastebin.com/tvZNCwN8
https://pastebin.com/XB4HwUsz
https://pastebin.com/bZDH1VHh
https://pastebin.com/xCPMHyqK
https://pastebin.com/mJJ4U9bH
https://pastebin.com/HrakBMBz
https://pastebin.com/GN9npU0W
https://pastebin.com/nr9hJd8p
https://pastebin.com/2CgD5tqb
https://pastebin.com/tXtkUfHd
https://pastebin.com/wkFA8iGP
https://pastebin.com/7z45qQzr
https://pastebin.com/w5RGPRen
https://pastebin.com/WLJewYNA
https://pastebin.com/bEbNLpBi
https://pastebin.com/Chn8rUkh
https://pastebin.com/jz4F2APq
https://pastebin.com/iRWuPNFz
https://pastebin.com/GmHAMH6f
https://pastebin.com/uJMGcsEG
https://pastebin.com/jPB68a0D
https://pastebin.com/MjC66wkv
https://pastebin.com/S7RDTAM9
https://pastebin.com/0bvuDUM1
https://pastebin.com/8czdKG36
https://pastebin.com/usWbPMRE
https://pastebin.com/kywEMqJp
https://pastebin.com/XJFyGMVu
https://pastebin.com/QZm2TaPm
https://pastebin.com/Qtuytbx1
https://pastebin.com/JCBYPdpc
https://pastebin.com/cVLahV0P
https://pastebin.com/0na2hGU9
https://pastebin.com/qpAgP7Mi
https://pastebin.com/k75PXkp7
https://pastebin.com/DZtU9neL
https://pastebin.com/7Yz1D9Fq
https://pastebin.com/xCwndzDR
https://pastebin.com/bGRUESTt
https://pastebin.com/fsnvCaCQ
https://pastebin.com/NcTVLKBf
https://pastebin.com/dXFykLyA
https://pastebin.com/N2w0m4pJ
https://pastebin.com/XEcYb0wP
https://pastebin.com/r7ZVGBz0
https://pastebin.com/Tz3svy7W
https://pastebin.com/CV8Ety7V
https://pastebin.com/sE3tU6ZT
https://pastebin.com/fCeY1t0n
https://pastebin.com/NXaPbSUG
https://pastebin.com/qt5yxi3M
https://pastebin.com/aTKPHQrG
https://pastebin.com/9SAkknhv
https://pastebin.com/Jx0dN32i
https://pastebin.com/7SyfLKbJ
https://pastebin.com/EX2bMjNe
https://pastebin.com/tdx0tmft
https://pastebin.com/gg9V1ytb
https://pastebin.com/BrBEcy26
https://pastebin.com/PZjmp9Bq
https://pastebin.com/k2TdbGrr
https://pastebin.com/yTV7fVKm
https://pastebin.com/P1a8Qn8J
https://pastebin.com/aekNrH9G
https://pastebin.com/LHvEPcc4
https://pastebin.com/LNzusSGs
https://pastebin.com/sMeA2PNw
https://pastebin.com/kRSKWe7H
https://pastebin.com/T8hXpM8Q
https://pastebin.com/STXJwKnY
https://pastebin.com/EZMpBXrs
https://pastebin.com/eqK7fyHg
https://pastebin.com/JJaAFQJa
https://pastebin.com/DrZAxjY8
https://pastebin.com/QFimQBUz
https://pastebin.com/z1FcAaJv
https://pastebin.com/7BcKWKaL
https://pastebin.com/FcphACWj
https://pastebin.com/BLd4XZSU
https://pastebin.com/NdHCmLXc
https://pastebin.com/ufeKaAdj
https://pastebin.com/U2fQcdaL
https://pastebin.com/XDbEhrEt
https://pastebin.com/gscukzCs
https://pastebin.com/epqMfKhi
https://pastebin.com/n6kDJLQe
https://pastebin.com/K3pCsdsT
https://pastebin.com/29L18WQV
https://pastebin.com/dweu6hGY
https://pastebin.com/i5UhDU4b
https://pastebin.com/4hqqvGJ3
https://pastebin.com/EPm7s12S
https://pastebin.com/L8iZAZmK
https://pastebin.com/jv4NNhvR
https://pastebin.com/igpmbMZv
https://pastebin.com/a3Q6qpTQ
https://pastebin.com/hq9kLWQN
https://pastebin.com/uSD7HbRh
https://pastebin.com/Q04Dqr3D
0 个评论
要回复文章请先登录或注册