navPathStack.setInterception({
willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar",
operation: NavigationOperation, animated: boolean) => {
let target: NavDestinationContext = to as NavDestinationContext;
LogUtils.info("跳到="+target.pathInfo.name)
BaseUtils.isLogin().then((login:boolean)=>{
if (!login) {
if (target.pathInfo.name == RouterPath.FaultReportingPage) {
LogUtils.info("有的页面需要登录,没登录就先关闭,再去登录")
target.pathStack.pop();
target.pathStack.pushPathByName(RouterPath.login, null);
}
}
})
}
})
}
这个感觉不像是拦截,因为它是先执行了目标界面的aboutToAppear,有没有那种跳转之前就拦截的。
1、依据代码描述场景,参考demo:
1、上面代码,处理时机,在这俩个函数中可拦截,跳转前拦截,返回拦截。这里可作为您业务逻辑的统一拦截管理,在aboutToAppear 执行前。
2、aboutToAppear 生命周期参考:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-page-custom-components-lifecycle-V5