angular - 为什么 CanDeactivate Guard 无法读取我的组件中的方法?
问题描述
我正在懒惰地加载我的路线。我正在尝试CanDeactivate
按照文档使用,但似乎警卫无法读取组件的属性或方法。
这是我的守护
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<PatientRegistrationFormComponent> {
constructor() {}
canDeactivate(component: PatientRegistrationFormComponent) {
return component.isChangesSaved();
}
}
isChangesSaved()
是组件内部的一个方法。
public isChangesSaved(): Observable<boolean> {
if (this.isDirtyForm()) {
return this.confirmService.confirmUnsavedChanges();
}
return of(false);
}
这是错误,
错误错误:未捕获(承诺中):TypeError:无法读取 null 的属性“isChangesSaved” TypeError:无法在 CanDeactivateGuard.push../src/app/core/database/database-cores/registry- 读取属性“isChangesSaved”的 null core/guards/patient-registration.guard.ts.CanDeactivateGuard.canDeactivate
这是我的模块(延迟加载)
@NgModule({
imports: [
CommonModule,
RegistryRoutingModule,
ReactiveFormsModule,
FormsModule,
],
declarations: [
RegistryContainerComponent,
RegistryHomeComponent,
PatientRegistrationFormComponent, // component of interest ….
],
exports: [PatientRegistrationFormComponent],
schemas: [NO_ERRORS_SCHEMA],
providers: [
…fromGaurds.guards, // all my guards are loaded here….
{ provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher },
ToastService,
],
})
export class RegistryModule {}
这是我的路由模块
const routes: Routes = [
{
path: '',
component: RegistryContainerComponent,
children: [
{
path: '',
component: RegistryHomeComponent,
},
{
path: 'patientregistration',
component: PatientRegistrationFormComponent,
canDeactivate: [fromGaurds.CanDeactivateGuard],
children: [
{
path: '',
component: PatientRegistrationFormComponent,
canActivate: [fromGaurds.PatientRegistryGuard],
canDeactivate: [fromGaurds.CanDeactivateGuard],
},
{
path: ':id',
component: PatientRegistrationFormComponent,
canActivate: [fromGaurds.PatientRegistryRecordGuard],
canDeactivate: [fromGaurds.CanDeactivateGuard],
},
],
},
],
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class RegistryRoutingModule {}
这是接口的错误还是错误的实现?
更新
我厌倦了将守卫添加到延迟加载模块。RegistryModule
这是上面提到的子模块的父模块,
const dbRoutes: Routes = [
{
path: '',
component: DbHomeComponent,
data: { preload: true },
canDeactivate: [fromGaurds.CanDeactivateGuard],
children: [
{ path: '', component: UsersDashboardComponent },
{
path: 'registry',
canActivate: [RoleGuardService],
data: { expectedRole: { roles: ['reg'] } },
loadChildren: '../database-cores/registry-core/modules/registry.module#RegistryModule',
},
{
path: 'cloudstorage',
canActivate: [RoleGuardService],
loadChildren:
'../database-cores/cloud-storage-core/modules/cloud-storage.module#CloudStorageModule',
},
],
},
];
@NgModule({
imports: [RouterModule.forChild(dbRoutes)],
exports: [RouterModule],
})
export class DbRoutingModule {}
@NgModule({
declarations: [
DbHeaderComponent,
DbFooterComponent,
DbHomeComponent,
UsersDashboardComponent,
],
imports: [
}),
],
exports: [],
providers: [
...fromGaurds.guards,
MDBSpinningPreloader,
ToastService,
DirectoryService,
],
schemas: [NO_ERRORS_SCHEMA],
})
export class DbModule {}
解决方案
挑出来。归功于sliceofbytes
问题还在于在父路径和子路径中复制我的组件
以下配置也可以按预期工作。canDeactivate
服务于两个孩子的路径。我相信核心问题是我只有一个实例为PatientRegistrationFormComponent
两条子路由提供服务。
这是一个回购。
const routes: Routes = [
{
path: '',
component: RegistryContainerComponent,
children: [
{
path: '',
component: RegistryHomeComponent,
},
{
path: 'patientregistration',
//component: PatientRegistrationFormComponent, <---- here was the core issue
children: [
{
path: '',
component: PatientRegistrationFormComponent,
canActivate: [fromGaurds.PatientRegistryGuard],
canDeactivate: [fromGaurds.CanDeactivateGuard],
},
{
path: ':id',
component: PatientRegistrationFormComponent,
canActivate: [fromGaurds.PatientRegistryRecordGuard],
canDeactivate: [fromGaurds.CanDeactivateGuard],
},
],
},
],
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class RegistryRoutingModule {}
推荐阅读
- javascript - @click 调用在 vue.js 中不起作用的方法
- ssl - 如何在 GCP 中激活 SSL 证书。?
- javascript - 如何替换 AdjacentHTML 而不是创建另一个 AdjacentHTML 实例
- python - ValueError :当数组长度与索引长度不匹配时。如何调试这个?
- c# - 结构字段的加载值指令与加载地址指令的效率
- android - Jetpack Compose 应该如何引用可组合的高阶函数?
- visual-studio-code - 未找到“extension.LiveServer.goOnline”且未找到“liveSass.command.watchMySass”
- python - 用于有效地将条件应用于数组的 numpy 替代方案
- r - 有多少总统在世?
- python-3.x - 尝试使用 CMD 启动时输入当前不起作用