2016-11-10 49 views
1

假設我有以下路線:訂閱基於路線導航中角2數據變化

[ 
    { 
     path: 'view', 
     children: [ 
      { 

       path: ':id', 
       component: CustomerViewComponent, 
       resolve: { 
        customerViewData: CustomerResolve, 
        edit: ViewRouteResolve // Returns false 
       }, 
       data: { 
        other: false 
       }, 
       children: [ 
        { 
         path: 'edit', 
         resolve: { 
          edit: EditRouteResolve // Returns true 
         }, 
         data: { 
          other: true 
         } 
        }, 
        { 
         path: '', 
         data: { 
          other: false 
         } 
        } 
       ] 
      } 
     ] 
    }, 
    { path: '', component: CustomerListComponent } 
] 

我想使用CustomerViewComponent爲/視圖/和/視圖/ 1 /編輯

的問題是我無法在我的組件中捕獲這些數據更改。我曾嘗試與resolvedata,我不能捕獲任何變化......如預期

此代碼將不會觸發:

this.route.data.subscribe(m => { 
    debugger; // Triggers only once the view loads, then never again 
}); 

// This triggers quite often, but the data is always stale. 
this.router.events.subscribe(m => { 
    console.log(this.route.snapshot.data['edit']); 
    console.log(this.route.snapshot.data['other']); 
    debugger; 
}); 

難道這是一個錯誤?我唯一的解決辦法是看事件NavigationEnd和分析的.url字符串屬性...

回答

2

會轉而使用ActivatedRoute@angular/router

this.activatedRoute.params.subscribe(params => { 
    console.log(params['edit']); 
    console.log(params['other']); 
}); 
+0

我很欣賞的答案。 'this.route'實際上是'ActivatedRoute',我沒有看'params',而是'data'。 – jsgoupil

+1

哦,對。然後看看這個答案http://stackoverflow.com/a/38652281/3159399 –

+0

哇,這是一個這樣一個醜陋的設計...我一直在瀏覽GitHub上的錯誤,他們都通過設計關閉它們。有多人報告同樣的問題......這是一個設計問題。 – jsgoupil