HarmonyOS 关于NavPathStack疑问

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-observer-V5#observeronnavdestinationupdate我按照上述的无感监听去监听页面切换,发现无法监听到navigation页面,只能监听子页面,无法满足我的需求

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#%E7%A4%BA%E4%BE%8B2根据上面文档的示例2,我发现通过this.pageInfos.setInterception方法可以拿到所有页面的来源和目标,我想问一下,如何将这个能力集成到我utils.ts的工具类中,目前看起来只能在ets文件中使用

HarmonyOS
2024-12-25 08:25:22
470浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

1.根页面的监听,可以通过导航栏显示状态来判断

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#onnavbarstatechange9

.onNavBarStateChange((flag) => {
  if (flag) {
    console.log('NavBAR', 'Navigation Index change,now is show')
  } else {
    console.log('NavBAR', 'Navigation Index change,now is hide')
  }
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

2.在根页面可以将pageInfos通过AppStorage存起来,在utils取出来使用

根页面

import navigationLisenerUtils from '../../utils/NavigationLisenerUtils'

pageInfos: NavPathStack = new NavPathStack()
isUseInterception: boolean = false;
aboutToAppear(): void {
  AppStorage.setOrCreate(""pageInfos"",this.pageInfos)
}

Button('use interception', { stateEffect: true, type: ButtonType.Capsule })
  .width('80%')
  .height(40)
  .margin(20)
  .onClick(() => {
    /*this.isUseInterception = !this.isUseInterception;
    if (this.isUseInterception) {
      this.registerInterception();
    } else {
      this.pageInfos.setInterception(undefined);
    }*/
    this.isUseInterception = !this.isUseInterception;
    console.error(""this.isUseInterception:""+this.isUseInterception);
    if (this.isUseInterception) {
      // this.registerInterception();
      navigationLisenerUtils.registerInterception(this.isUseInterception);
      console.error(""action"");
    } else {
      this.pageInfos.setInterception(undefined);
    }
  })
  • 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.

utils

export class NavigationLisenerUtils{
  // isUseInterception: boolean = false;
  registerInterception(isUseInterception: boolean) {
    let pageInfos = AppStorage.get(""pageInfos"") as NavPathStack
    pageInfos.setInterception({
      willShow: (from: NavDestinationContext | ""navBar"", to: NavDestinationContext | ""navBar"",
      operation: NavigationOperation, animated: boolean) => {
        if (!isUseInterception) {
          return;
        }
        if (typeof to === ""string"") {
          console.log(""target page is navigation home"");
          return;
        }
        // redirect target page.Change pageTwo to pageOne.
        let target: NavDestinationContext = to as NavDestinationContext;
        if (target.pathInfo.name === 'pageTwo') {
          target.pathStack.pop();
          target.pathStack.pushPathByName('pageOne', null);
        }
      },
    didShow: (from: NavDestinationContext | ""navBar"", to: NavDestinationContext | ""navBar"",
    operation: NavigationOperation, isAnimated: boolean) => {
      if (!isUseInterception) {
        return;
      }
      if (typeof from === ""string"") {
        console.log(""current transition is from navigation home"");
      } else {
        console.log(`current transition is from  ${(from as NavDestinationContext).pathInfo.name}`)
      }
      if (typeof to === ""string"") {
        console.log(""current transition to is navBar"");
      } else {
        console.log(`current transition is to ${(to as NavDestinationContext).pathInfo.name}`);
      }
      },
      modeChange: (mode: NavigationMode) => {
        if (!isUseInterception) {
          return;
        }
        console.log(`current navigation mode is ${mode}`);
      }
    })
  }
}
let navigationLisenerUtils = new NavigationLisenerUtils()
export default navigationLisenerUtils as NavigationLisenerUtils
  • 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.
分享
微博
QQ
微信
回复
2024-12-25 10:04:59
相关问题
HarmonyOS关于RichText的疑问
1807浏览 • 1回复 待解决
HarmonyOS 关于deliveryWithInstall的疑问
1681浏览 • 1回复 待解决
关于NavPathStack的跳转问题
1431浏览 • 1回复 待解决
HarmonyOS 关于手动签名的疑问
1524浏览 • 1回复 待解决
HarmonyOS 关于混淆的疑问
1001浏览 • 1回复 待解决
关于鸿蒙next相关疑问
320浏览 • 0回复 待解决
关于鸿蒙next相关疑问
406浏览 • 1回复 待解决
关于鸿蒙next相关疑问
328浏览 • 0回复 待解决
关于鸿蒙next相关疑问
308浏览 • 1回复 待解决
关于鸿蒙next相关疑问
305浏览 • 0回复 待解决
关于鸿蒙next相关疑问
309浏览 • 0回复 待解决
关于鸿蒙next相关疑问
288浏览 • 0回复 待解决
关于鸿蒙next相关疑问
292浏览 • 0回复 待解决
关于鸿蒙next相关疑问
483浏览 • 1回复 待解决
关于鸿蒙next相关疑问
291浏览 • 0回复 待解决
关于鸿蒙next相关疑问
328浏览 • 0回复 待解决
关于鸿蒙next相关疑问
319浏览 • 0回复 待解决
关于鸿蒙next相关疑问
302浏览 • 0回复 待解决
关于鸿蒙next相关疑问
478浏览 • 1回复 待解决
HarmonyOS 关于AvPlayer中usage的问题疑问
963浏览 • 1回复 待解决
HarmonyOS 关于ArkTS UI样式文件的疑问
1223浏览 • 1回复 待解决
HarmonyOS 关于半模态转场的疑问
890浏览 • 1回复 待解决
HarmonyOS 关于DES加解密的疑问
877浏览 • 1回复 待解决
关于鸿蒙应用开发的疑问?
6162浏览 • 1回复 待解决
关于Wearable开发的几个疑问
8414浏览 • 2回复 待解决