Angular 2拦截路由事件,然后稍后路由

tt9*_*tt9 8 angular

我有一个角度2应用程序,并希望拦截路线事件,防止它们发生,播放动画,然后继续路由.

我的想法是这样的

如果我有课

export class SomeComponent {
    constructor(private router: Router){
        router.events.subscribe(evt => {
            if(evt instanceof NavigationStart){
                //I would like to cancel the event here the first time, and then 
                //play an animation, after the animation is done, trigger the router
                //to go to the original route it wanted to.
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法阻止该路由器完成导航过程?

Mad*_*jan 10

您可以CanActivate为父路径创建一个保护,您可以根据某个全局变量停止导航.该变量可以具有基于动画是否已显示的值.

所以你可能会做的是,

export class AnimationGuard implements CanActivate {
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    if(HasAnimationRun){
      return true;
    }
    runAnimation(state.url);
    return false;
  }
}

runAnimation(url){
  // run animation
  // set global variable.
  // navigate to url
}
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多关于CanActivate的信息.

希望这可以帮助!!