简体   繁体   English

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

[英]How can I take variable “data” from Routes in Angular 4?

Could you give me an example how to get data from Routes. 您能否举一个例子,说明如何从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 }
];

Thank you 谢谢

You can use ActivatedRoute in your component/service constructors. 您可以在组件/服务构造函数中使用ActivatedRoute

For example: 例如:

public class MyAppComponent{
data: any;   

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

You can find more details here . 您可以在此处找到更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM