我想我根本无法理解 Angular 2 路由。我的应用程序中有这样的结构:
const routes: Routes = [
{
path: 'login',
component: LoginViewComponent
},
{
path: 'main',
component: MainComponent,
children:
[
{
path: 'unit_list',
component: ListViewComponent
},
{
path: 'unit_info',
component: UnitInfoComponent,
children:
[
{
path: 'patient',
component: PatientDetailsComponent
}
]
}
]
},
{
path: '',
redirectTo: 'main',
pathMatch: 'full'
}
];
这是我的 app.module 的样子:
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
MainModule,
routing
],
declarations: [
AppComponent,
LoginViewComponent
],
bootstrap: [AppComponent]
})
和main.module
@NgModule({
imports: [
CommonModule,
FormsModule,
RouterModule
],
declarations: [
ListViewComponent,
MainComponent,
PatientDetailsComponent,
UnitInfoComponent,
]
})
问题是我无法让 router.navigateByUrl 函数正常工作。
我在 PatientDetailsComponent 中有 ngOnInit() 函数,它检查病人是否为空,如果是则我称之为“this.router.navigateByUrl("/main/unit_info")”,但它在标题中抛出错误:“无法激活已激活的插座”。它在 Angular 2.1.1 中工作正常,但当我升级到 2.4.X 时,似乎没有一个能解决我的问题。我也不确定这个路由是否正确,因为我有点迷失。
这是升级到 2.4.X 之前我的整个路由的样子:http://pastebin.com/Y377STcW http://pastebin.com/Y377STcW