2017-10-13 61 views
-1

你能否舉個例子來說明如何從路由中獲取數據。 Routing & Navigation如何從Angular 4中的路由中獲取變量「數據」?

const appRoutes: Routes = [ 
    { path: 'crisis-center', component: CrisisListComponent }, 
    { path: 'hero/:id',  component: HeroDetailComponent }, 
    { 
    path: 'heroes', 
    component: HeroListComponent, 
    data: { title: 'Heroes List' } // <--- How to use this data? 
    }, 
    { path: '', 
    redirectTo: '/heroes', 
    pathMatch: 'full' 
    }, 
    { path: '**', component: PageNotFoundComponent } 
]; 

謝謝

+0

在鏈接頁面中有一段:第三條路線中的數據屬性是存儲與此特定路線相關的任意數據的地方。數據屬性可在每條激活的路由中訪問。用它來存儲項目,如頁面標題,麪包屑文本和其他只讀的靜態數據。您將在後面的指南中使用解析警衛來檢索動態數據。「 DV缺乏自我研究 –

回答

1

您可以在組件/服務構造函數使用ActivatedRoute

例如:

public class MyAppComponent{ 
data: any; 

constructor(route: ActivatedRoute){ 
     this.data = route.snapshot.data; 
    } 
} 

你可以找到更多的細節here