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

前端之家收集整理的这篇文章主要介绍了Angular 2拦截路由事件,然后稍后路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个角度2应用程序,并希望拦截路线事件,防止它们发生,播放动画,然后继续路由.

我的想法是这样的

如果我有课

  1. export class SomeComponent {
  2. constructor(private router: Router){
  3. router.events.subscribe(evt => {
  4. if(evt instanceof NavigationStart){
  5. //I would like to cancel the event here the first time,and then
  6. //play an animation,after the animation is done,trigger the router
  7. //to go to the original route it wanted to.
  8. }
  9. })
  10. }
  11. }

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

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

所以你可能会做的是,

  1. export class AnimationGuard implements CanActivate {
  2. canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot) {
  3. if(HasAnimationRun){
  4. return true;
  5. }
  6. runAnimation(state.url);
  7. return false;
  8. }
  9. }
  10.  
  11. runAnimation(url){
  12. // run animation
  13. // set global variable.
  14. // navigate to url
  15. }

了解有关CanActivate here的更多信息.

希望这可以帮助!!

猜你在找的Angularjs相关文章