Angular LazyLoading模块路线的角度4按钮链接不工作
我有一个按钮,带有一个到延迟加载模块的routerLink指令。但是,链接不起作用。当它被加载到浏览器地址栏(当点击按钮时)时,该组件从未被加载,并且在控制台中我得到一个404错误。我做错了什么 我想要实现的功能是:当我转到/events/5获取一个已填写的表单时(从id为5的事件中进行编辑/更新),而当我转到/events/new获取一个空表单以插入一个新事件时 我想问题可能出在IncidentsRoutingModule中,但是我无法工作 我的主要应用程序路由模块如下所示:Angular LazyLoading模块路线的角度4按钮链接不工作,angular,lazy-loading,angular-router,Angular,Lazy Loading,Angular Router,我有一个按钮,带有一个到延迟加载模块的routerLink指令。但是,链接不起作用。当它被加载到浏览器地址栏(当点击按钮时)时,该组件从未被加载,并且在控制台中我得到一个404错误。我做错了什么 我想要实现的功能是:当我转到/events/5获取一个已填写的表单时(从id为5的事件中进行编辑/更新),而当我转到/events/new获取一个空表单以插入一个新事件时 我想问题可能出在IncidentsRoutingModule中,但是我无法工作 我的主要应用程序路由模块如下所示: const ap
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 {}
附带模块为:
@NgModule({
imports: [
CommonModule,
FormsModule,
IncidentsRoutingModule
],
declarations: [
IncidentsComponent,
IncidentDetailsComponent,
IncidentsListComponent
],
providers:[IncidentsService]
})
export class IncidentsModule { }
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 { }
IncidentsRoutingModule是:
@NgModule({
imports: [
CommonModule,
FormsModule,
IncidentsRoutingModule
],
declarations: [
IncidentsComponent,
IncidentDetailsComponent,
IncidentsListComponent
],
providers:[IncidentsService]
})
export class IncidentsModule { }
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指令的按钮,但该按钮不起作用。以下是我的意外事件组成部分:
@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() {
}
}
@组件({
//选择器:“应用程序事件”,
模板:`
Περιστατικά
Νέο Περιστατικό
`,
样式URL:['./incents.component.css']
})
导出类意外事件组件{
构造函数(){
}
}
将其更改为
<button type="button" class="btn btn-outline-primary" style="margin: 20px" routerLink="new">
<i class="fa fa-plus-circle" style="margin-right: 10px"></i>
Νέο Περιστατικό
</button>
Νέο Περιστατικό
您可以尝试在主路由模块的forRoot方法中添加
useHash:true
问题正如我在IncidentsRoutingModule中所怀疑的那样。我将其更改为以下内容,并按预期工作
const incidentsModuleRoutes: Routes = [
{
path: '',
component: IncidentsComponent,
children: [
{
path: '', component: IncidentsListComponent,
},
{
path:':id', component: IncidentDetailsComponent
},
{
path: 'new', component: IncidentDetailsComponent
}
]
}
];
显然,我所理解的(不知道这是否正确,没有找到它或在什么地方读过它)是,延迟加载模块中的子级引用了
声明
在我的情况下,我只有一个
内部事故组件。因此,所有路径都必须位于IncidentsComponent下的子对象中。在其他子组件下添加子对象我认为只有当这些组件也有
声明时,这种方法才有效??我说得对吗???你说不工作是什么意思?得到错误?是的404发现不完全相同。浏览器url得到更新,但我得到错误:加载资源失败:服务器响应状态为404()。有什么想法吗?编辑了我的答案