在Angular 4中定义路线

时间:2017-08-29 06:28:30

标签: angular typescript mean-stack

在Angular 4文档中,路由的代码片段如下所示:

导入模块:

import { RouterModule, Routes } from '@angular/router';

路由示例:

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

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    )
    // other imports here
  ],
  ...
})
export class AppModule { }

我的问题是为什么我们必须将路线定义为const appRoutes: Routes = [...]而不是const appRoutes = [...],它似乎也是如此。

2 个答案:

答案 0 :(得分:5)

这就是为什么要使用Typescript,为了给出强类型,以便我们知道变量的类型, 不提供任何/空白,默认为any。这就是它仍然有效的原因。 通过不使用类型,您可以消除Angular中的Typings的优势。

答案 1 :(得分:1)

由于TypeScript是一种类型化语言,因此最好定义变量类型,以便自动完成/编译错误检测能够正常工作。

相关问题