定義路由在角度4文檔上路由的代碼段被給出爲如下:在角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 = [...]
,它似乎也是這樣工作的。
我熟悉類型,布爾值,數字,字符串等等的變量。上例中是一個「自定義」類型的路由被創建?這與導入的路線模塊有任何關係嗎? – DanT29
@ DanT29這個對象數組是一種路由。路由器模塊在啓動時需要一組路由,所以我們導入數組 –