2016-10-24 2 views
1

페이지 새로 고침시 '/ splash'경로로 탐색을 강제로 할 수 있습니까?페이지 새로 고침시 라우터를 재설정하는 방법은 무엇입니까?

'/ discover'에서 자신을 발견하고 페이지를 새로 고치면 응용 프로그램이 처음부터 다시 시작하는 것을 선호합니다 (/ splash).

const appRoutes: Routes = [ 
    { path: '', redirectTo: '/splash', pathMatch: 'full' }, 
    { path: 'splash'    , component: SplashComponent  }, 
    { path: 'discover'   , component: DiscoverComponent  }, 
    { path: 'broadcast'   , component: BroadcastComponent  }, 
    { path: 'notifications'  , component: NotificationsComponent }, 
    { path: 'notifications/:key' , component: NotificationComponent } 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

내 부모 AppComponent의 OnInit & OnDestroy 방법 내에서 router.navigate 통화를 시도했습니다

그러나 무시 나타납니다.
ngOnInit() : void { 
    this.router.navigate(['splash']); 
} 

는 또한 라우터 자체의 OnDestroy 방법 내에서 탐색 통화를 시도했습니다.

+0

당신은 웹 서버의 어떤 종류를 사용합니까? 아파치? – BeetleJuice

답변

1

당신은 당신의 AppComponent 생성자에서 라우터의 첫 번째 네비게이션 이벤트에 가입하여 작업을 수행 할 수 있습니다

import 'rxjs/add/operator/filter'; 
    import { Router, NavigationStart } from '@angular/router'; 

    export class AppComponent { 
     private subscription: any; 

     constructor(private router: Router) { 
     this.subscription = this.router.events 
      .filter(e => e instanceof NavigationStart && e.id === 1) 
      .subscribe(e => { 
      this.redirectToSplash(); 
      }); 
     } 

     private redirectToSplash() { 
     this.router.navigate(["splash"]); 
     this.subscription.unsubscribe(); 
     } 
    } 
관련 문제