2016-11-10 69 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