2017-01-17 12 views
5

私は角度2のアプリケーションを持っており、ルートイベントを傍受し、起こらないようにし、アニメーションを再生してからルートを継続したいと考えています。角度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. 
      } 
     }) 
    } 
} 

を持っている場合は、ナビゲーション処理を終えたから、そのルータを停止する方法があります。この

ですか?

+0

URLに '?showAnimation'のようなものを追加できますか?URLの変更が発生するたびに、そのフラグがあるかどうかを確認し、そうであれば元のルートのアドレスに移動する前にアニメーションを行いますか? – Pytth

答えて

8

親ルートのガードを作成することができます.Global変数に基づいてナビゲーションを停止することができます。変数には、アニメーションが表示されているかどうかに基づいて値が設定されます。ですので、あなたが何も

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 
} 

がCanActivate hereについては、こちらをご覧ください。

これが役立ちますように!

関連する問題