HarmonyOS Navigation在onPop回调中打开新页面异常

navPathStack: NavPathStack 
navPathStack.pushPath( { 
  name: 'path1', 
  param: 'param1', 
  onPop: () => { 
    // 在onPop回调中打开新的path1,失败 
    navPathStack.pushPath({name: 'path1', param: 'param2'}) 
  } 
  )
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

以上代码,在path1的onPop回调中,打开新的path1,打开失败。

HarmonyOS
2024-08-29 11:36:17
1129浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

提供demo代码如下:

@Component 
struct Page01 { 
  @Consume('pageInfos') pageInfos: NavPathStack; 
  build() { 
    NavDestination() { 
      Button('push Page03').width('80%').onClick(() => { 
        this.pageInfos.pushPathByName('Page03','',(popInfo: PopInfo)=>{ 
          console.log(`Page03被弹出啦!!!`) 
        }); 
      }).margin({ top: 10, bottom: 10 }) 
      Button('pop Page01').width('80%').onClick(() => { 
        //将处于栈顶的一个元素弹出 
        this.pageInfos.pop({number:1}) 
      }).margin({ top: 10, bottom: 10 }) 
    }.title('Page01')}} 
@Component 
struct Page03 { 
  @Consume('pageInfos') pageInfos: NavPathStack; 
  build() { 
    NavDestination() { 
      Button('pop Page03').width('80%').onClick(() => { 
        //弹出栈顶元素 
        this.pageInfos.pop({number:1}) 
      }).margin({ top: 10, bottom: 10 }) 
    }.title('Page03')}} 
 
@Entry 
@Component 
struct IndexPage { 
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack() 
  @Builder 
  PagesMap(name: string) { 
    if (name == 'Page01') { 
      Page01() 
    }  else if (name == 'Page03') { 
      Page03() 
    } 
  } 
  build() { 
    Navigation(this.pageInfos) { 
      Button('push Page01').width('80%').onClick(() => { 
        this.pageInfos.pushPathByName('Page01','',()=>{ 
          //Page01被弹出后,将page03加入栈顶 
          this.pageInfos.pushPathByName('Page03','') 
        }); 
      }) 
    }.mode(NavigationMode.Stack) 
     .titleMode(NavigationTitleMode.Mini) 
     .title('主页').navDestination(this.PagesMap)}}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

当前HarmonyOS是不支持“弹出栈顶元素时通过回调函数中重复将同一元素继续推入栈顶”,所以说提出的问题不属于系统bug范畴;可以使用之前给出的replace实现类似的效果,重建一个当前页面栈并传入参数。链接如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#replacepathbyname11

分享
微博
QQ
微信
回复
2024-08-29 18:22:40
相关问题
HarmonyOS Navigation先pop再push新页面失败
592浏览 • 1回复 待解决
HarmonyOS 如何刷新页面内容
616浏览 • 1回复 待解决
如何更新页面列表数据
7975浏览 • 1回复 待解决
HarmonyOS 数据改变未刷新页面
1310浏览 • 0回复 待解决
怎么进度条更新的时候刷新页面
5573浏览 • 1回复 待解决
dialog跳转新页面返回后dialog关闭
1026浏览 • 1回复 待解决
HarmonyOS Scroll组件onScroll异常
602浏览 • 1回复 待解决