HarmonyOS鸿蒙Next中NavDestination的onReady入参context是空对象该怎么解决?
HarmonyOS鸿蒙Next中NavDestination的onReady入参context是空对象该怎么解决?
@Entry @ComponentV2 struct Index {
@Local NavigationStack : NavPathStack = new NavPathStack() ;
@Builder navDestination_builder( name : string , param : string | null ) {
if( name === "boo" ) {
NavDestination() {
Text( "This is boo!!" )
.fontSize( 25 )
}
.height( "100%" )
.width( "100%" )
.onReady(
( context : NavDestinationContext ) => {
// Here prints the context
console.log( "!!! Context:\n" + JSON.stringify( context , undefined , 2 ) ) ;
}
)
} else {
// "else" goes to here
NavDestination() {
Text( "Else page…" )
.fontSize( 25 )
Button( "Push \"boo\"" )
.onClick(
() => {
this.NavigationStack.pushPath( { name : "boo" , param : "Boo!!" } ) ;
console.log( "!!! Stack\n" + JSON.stringify( this.NavigationStack , undefined , 2 ) ) ;
}
)
}
.height( "100%" )
.width( "100%" )
}
}
build() {
Navigation( this.NavigationStack ) {
Button( "Push \"else\"" )
.onClick(
() => {
this.NavigationStack.pushPath( { name : "else" , param : null } ) ;
console.log( "!!! Stack\n" + JSON.stringify( this.NavigationStack , undefined , 2 ) ) ;
}
)
}
.height( "100%" )
.width( "100%" )
.navDestination( this.navDestination_builder )
}
}
简单写了个demo,其中onReady的context打印出来就是{}
,没搞太明白,是哪一步的姿势有问题?
实际写的时候destination的UI和逻辑包在在自定义组件里面,还是通过onReady获取参数方便些,但是一直获取不到就有些苦恼人了。
走过路过的大佬不要错过,我隔着网线给你啵一个😘
更多关于HarmonyOS鸿蒙Next中NavDestination的onReady入参context是空对象该怎么解决?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
貌似这代码输出是有值的,建议新建个工程试试
!!! Stack
{
"pathArray": [
{
"name": "else",
"param": null,
"index": 0,
"navDestinationId": "0",
"pushDestination": false
},
{
"name": "boo",
"param": "Boo!!",
"index": -1,
"pushDestination": false
}
],
"isReplace": 0,
"type": "NavPathStack",
"disableAllAnimation": false,
"animated": true,
"nativeStack": {},
"popArray": []
}
更多关于HarmonyOS鸿蒙Next中NavDestination的onReady入参context是空对象该怎么解决?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
麻烦大佬点进“boo”页面,!!! Context后面应该是空对象🧐,
不是空对象,你加个断点看一下。
抱歉忘回复了,后来才发现成员其实都是getter,所以打印不出来🙇,
Context原型上是有数据信息的,例如:context.pathInfo.name,但是由于JSON.stringify有明确的处理规则:它不会序列化对象的原型链(包括 proto)和不可枚举属性,所以JSON.stringify不支持原型链数据,所以context解析出空对象。
谢谢佬,我把这帖子给忘了,context的成员确实都是getter,当时没想明白🙇,
在HarmonyOS鸿蒙Next中,NavDestination的onReady回调参数context为空,通常是由于页面导航生命周期未正确初始化导致。请检查页面路由配置是否正确,确保NavDestination组件在导航栈中已完全加载。可通过在aboutToAppear或onPageShow生命周期中延迟触发导航操作,或使用setTimeout等待上下文就绪。确认导航参数传递是否符合规范,避免未定义或空值传递。
在HarmonyOS Next中,NavDestination
的onReady
回调参数NavDestinationContext
为空对象通常是因为当前导航栈中目标页面的上下文尚未完全初始化或未正确绑定。请检查以下两点:
- 确保
NavDestination
是在导航栈成功推送后创建的,避免在页面未挂载时访问上下文。 - 确认
NavDestination
的声明位置,如果通过动态构建(如navDestination_builder
),需确保参数传递和页面名称匹配正确。
建议在onReady
中增加异步延迟处理(如使用setTimeout
)或检查导航栈状态,确保上下文已就绪。若问题仍存在,可尝试在目标页面使用@Param
装饰器直接接收参数,替代依赖上下文的方式。