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
1天前
浏览
收藏 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')
  }
})

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);
    }
  })

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
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 关于deliveryWithInstall的疑问
705浏览 • 1回复 待解决
HarmonyOS关于RichText的疑问
859浏览 • 1回复 待解决
关于NavPathStack的跳转问题
688浏览 • 1回复 待解决
HarmonyOS 关于手动签名的疑问
543浏览 • 1回复 待解决
HarmonyOS 关于ArkTS UI样式文件的疑问
405浏览 • 1回复 待解决
HarmonyOS 关于DES加解密的疑问
31浏览 • 1回复 待解决
HarmonyOS 关于半模态转场的疑问
23浏览 • 1回复 待解决
关于Wearable开发的几个疑问
7552浏览 • 2回复 待解决
关于鸿蒙应用开发的疑问?
5461浏览 • 1回复 待解决
关于DependentLayout布局的一点疑问
5717浏览 • 1回复 待解决
关于鸿蒙camera外设的驱动编写疑问
4556浏览 • 1回复 待解决
请教一下关于Ticktimer 的疑问
3526浏览 • 1回复 待解决
关于智能穿戴应用开发的几个疑问
10821浏览 • 2回复 待解决
关于鸿蒙分布式数据库key疑问
5841浏览 • 1回复 待解决
HarmonyOS getRawFileContentSync 疑问
63浏览 • 1回复 待解决
关于网络状态的疑问
711浏览 • 1回复 待解决
NavPathStack使用问题
1691浏览 • 1回复 待解决