2017-03-14 2 views
2

각도 (2+) 라우터에는 rxjs 제목 인 events 속성이 있습니다. 일단 가입 각도 라우터 이벤트 이름

, 당신은 같은 이벤트를 수신 할 수 있습니다 :

router.events.subscribe((event: Event) => { console.log(event) });

하지만 문제는 당신이 모든 이벤트를 수신하기 때문에 이벤트 유형을 얻을 수있는 방법이 없습니다. 이 작업을 수행 할 수있는 더 좋은 방법이 있다면 내가 궁금

router.events.subscribe((event: Event) => { 
    if(event.constructor.name === 'NavigationStart') { 
    loadingService.start(); 
    } else if(event.constructor.name === 'NavigationEnd') { 
    loadingService.complete(); 
    drawerService.destroyAll(); 
    } 
}); 

: 해키 솔루션은이 같은 이벤트의 생성자의 이름을 검사 할 수있다?

답변

1

Event은 기본 클래스입니다.

0,123,516 :

구체적인 이벤트 값은

할 수 있습니다 예를 들어, 사용을 RoutesRecognized

  • NavigationStart
  • NavigationEnd
  • NavigationCancel
  • NavigationError

    • 중 하나입니다
      constructor(router:Router) { 
          router.events 
          .filter(event => event instanceof NavigationStart) 
          .subscribe((event:NavigationStart) => { 
           // You only receive NavigationStart events 
          }); 
      

      너무 또한 How to detect a route change in Angular 2?

  • +1

    감사를 참조하십시오! 문서를 다시 PR'd! https://github.com/angular/angular/pull/15142 – amcdnl

    +0

    가장 좋은 형태의 공헌 : –