如何从Angular 4中的Routes中获取变量“数据”?

时间:2017-10-13 14:44:19

标签: angular parameters routes

您能举例说明如何从路由获取数据。 Routing & Navigation

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 }
];

谢谢

1 个答案:

答案 0 :(得分:1)

您可以在组件/服务构造函数中使用ActivatedRoute

例如:

public class MyAppComponent{
data: any;   

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

您可以找到更多详情here