我的应用程序有一个标准的路由模块。但是,由于我的营销团队不断更改路径名称,因此我想使用常量驱动的路径名称而不是硬编码路径名称。为了实现相同的目的,我做了如下改动:
import { NgModule, OnInit } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CrisisListComponent } from './crisis-list.component';
import { HeroListComponent } from './hero-list.component';
import { PageNotFoundComponent } from './not-found.component';
import { ConstantsService } from './constants-service.ts';
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'heroes', component: HeroListComponent },
{ path: '', redirectTo: '/heroes', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
],
exports: [
RouterModule
]
})
export class AppRoutingModule implements OnInit {
constructor(private const: ConstantsService){}
const routeNames: Object;
ngOnInit(){
this.routeNames = this.const.getRouteNames();
// The constants service is a plain injectable that returns an object
// with key-value pairs for route names
}
}
此后我无法继续前进。由于ngOnInit
方法本身没有被调用。我试图在AppRoutingModule
声明中导出@ngModule
,但我得到一个堆栈超大的错误,即我正在某处进行递归调用。