我有一个带有指向延迟加载模块的 routerLink 指令的按钮。但是链接不起作用。当它加载到浏览器地址栏中(单击按钮时)时,组件永远不会被加载,并且在控制台中我收到 404 错误。我究竟做错了什么。
我想要实现的功能是:当我去 /incidents/5 获取一个填写好的表格(来自 id = 5 的事件用于编辑/更新),而当我去 /incidents/new 获取一个空表格来插入一个新事件。
我想问题可能出在 IncidentsRoutingModule 但我无法工作。
我的主要应用程序路由模块如下:
const appRoutes: Routes = [
{ path: '', redirectTo:'home', pathMatch:'full'},
{ path: 'home', component: HomeComponent },
{ path: 'incidents', loadChildren : 'app/incidents/incidents.module#IncidentsModule' },
{ path: 'patients', loadChildren: 'app/patients/patients.module#PatientsModule' },
{ path: 'doctors', loadChildren: 'app/doctors/doctors.module#DoctorsModule' },
{ path: 'clinics', loadChildren: 'app/clinics/clinics.module#ClinicsModule' },
{ path: 'search', component: SearchComponent },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes, { preloadingStrategy: PreloadAllModules } )
],
exports: [RouterModule]
})
export class AppRoutingModule {}
IncidentsModule 是:
@NgModule({
imports: [
CommonModule,
FormsModule,
IncidentsRoutingModule
],
declarations: [
IncidentsComponent,
IncidentDetailsComponent,
IncidentsListComponent
],
providers:[IncidentsService]
})
export class IncidentsModule { }
IncidentsRoutingModule 是:
const incidentsModuleRoutes: Routes = [
{
path: '',
component: IncidentsComponent,
children: [
{
path: '', component: IncidentsListComponent,
children: [
{
path:':id', component: IncidentDetailsComponent
},
{
path: 'new', component: IncidentDetailsComponent
}
]
}
]
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(incidentsModuleRoutes)
],
exports: [RouterModule]
})
export class IncidentsRoutingModule { }
我的 IncidentsComponent 有一个带有 routerLink 指令的按钮,该指令不起作用。贝娄是我的 IncidentsComponent:
@Component({
// selector: 'app-incidents',
template: `
<div class="container-fluid">
<h3 style="margin:20px 20px 0px 20px;">Περιστατικά</h3>
<button type="button" class="btn btn-outline-primary" style="margin: 20px" routerLink="/incidents/new">
<i class="fa fa-plus-circle" style="margin-right: 10px"></i>
Νέο Περιστατικό
</button>
<router-outlet></router-outlet>
</div>
`,
styleUrls: ['./incidents.component.css']
})
export class IncidentsComponent {
constructor() {
}
}