【问题标题】:How can I take variable "data" from Routes in Angular 4?如何从 Angular 4 中的路由中获取变量“数据”?
【发布时间】:2017-10-13 14:44:19
【问题描述】:

您能否举个例子,如何从 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 }
];

谢谢

【问题讨论】:

  • 在链接页面中有一个部分:'第三条路线中的数据属性是存储与此特定路线关联的任意数据的地方。在每个激活的路由中都可以访问 data 属性。使用它来存储页面标题、面包屑导航文本和其他只读静态数据等项目。您将在本指南后面使用解析保护来检索动态数据。 DV缺乏自我研究

标签: angular parameters routes


【解决方案1】:

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

例如:

public class MyAppComponent{
data: any;   

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

您可以找到更多详细信息here

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2018-06-13
    • 2016-05-13
    • 1970-01-01
    相关资源
    最近更新 更多