2016-10-11 5 views
1

私はルートコンポーネントを持っていて、その中に特別なセクション内にルートタイトルを表示したい。
すべてのコンポーネントでどのルートがアクティブであるかを検出できます。
しかし、私はルートコンポーネントの内部でそれを行う方法を知らない。ルートコンポーネント内にルート名を表示

このコードはChildComponent1のタイトル罰金を示しています

route:ActivatedRoute 
ngOnInit() { 

     this.route.data.subscribe(
      data => console.log(data) 
     ) 

    } 

は、どのように私は、現在のroute`sのタイトルが何であるかを検出し、ルートコンポーネントの内側にそれを表示することができますか?

export const routes: Routes = [ 

    {path: '', component: Component1, canActivate: [NonAuthGuard]}, 

    { 
    path: 'root', component: rootcomponent, canActivate: [AuthGuard], children: [ 
    { 
     path: 'Child1', 
     component: ChildComponent1, 
     data: { 
     title: 'Child1' 
     }, 
     canActivate: [AuthGuard], 
     canDeactivate: [CanDeactivateGuard] 
    }, 
    { 
     path: 'Child2', 
     component: ChildComponent2, 
     data: { 
     title: 'Child2' 
     }, 
     canActivate: [AuthGuard], 
     canDeactivate: [CanDeactivateGuard] 
    }, 
]} 
+0

UPD:ここで答えが見つかりましたhttp://stackoverflow.com/questions/38644314/changing-the-page-title-using-the-angular-2-new-router – norweny

答えて

0

あなたのルートコンポーネントにRouterを注入し、そのイベントを購読することができます。 例:

constructor(private router : Router) { 
    this.router.events.subscribe((event) => { 
     if(event instanceof NavigationStart) { 
      console.log(event); 
      console.log(event.url); 
     } 
    }); 
} 

あなたのイベントがいくつかの回を発生しますので、あなたのイベントは、NavigationStartまたはNavigationEndのインスタンスである場合は、チェックする必要があります。

+0

そして、ルートのデータタイトル?イベントオブジェクトには、IDとURLだけが含まれます。 {id:1、url: "/ url/url1"、urlAfterRedirects: "/ url/url1"} – norweny

+0

ルートのデータタイトルはどういう意味ですか? – micronyks

+0

私は、アプリケーション内をどのようにナビゲートするかを記述する、私のルート配列(ルート定数)を提供しました。各経路は、タイトル属性を有するデータフィールドを有する。 – norweny

関連する問題