我想将多个组件(Component1 \ Component2)传递到我的canDeactivate保护程序中,所以我使用如下所示的泛型。
Problem --我想从传入的每个组件中访问一个属性,那么如何获得泛型组件属性?
我一定要演吗?我可以在调试器中看到组件在那里,但在函数活动之前,我无法访问这些属性。
FYI - component.profileForm.dirty
不工作,因为它不知道T是什么,直到我在函数中
export class PreventUnsavedChangesGuard <T> implements CanDeactivate <T> {
constructor(private confirmService: ConfirmService) {}
canDeactivate(component: T): Observable < boolean > | boolean {
if (component.hasOwnProperty('profileForm')) {
if (component.profileForm.dirty) {
return this.confirmService.confirm();
}
}
return true;
}
}
发布于 2020-11-22 00:01:45
您可以定义一个包含您期望组件具有的最小信息的接口,例如,
interface HasProfileForm {
profileForm: Form
}
然后你可以修改你的护卫以使用新的界面,例如,
export class PreventUnsavedChangesGuard <T extends HasProfileForm> implements CanDeactivate <T> {
constructor(private confirmService: ConfirmService) {}
canDeactivate(component: T): Observable<boolean> | boolean {
if (component.hasOwnProperty('profileForm')) {
if (component.profileForm.dirty) {
return this.confirmService.confirm();
}
}
return true;
}
}
https://stackoverflow.com/questions/64950934
复制相似问题