我需要创建 canDeactiovateGuard 以防止用户在尝试离开表单页面时丢失表单数据。下面的代码不起作用,因为 return canDeactivate 在 canDeactivate = 确认之前起作用。我不知道如何解决这个问题。
@Injectable()
export class CanDeactivateGuard implements CanDeactivate<Component> {
canDeactivate(component: Component): boolean {
let canDeactivate: boolean;
if (component.formValueChanged) {
component.confirmService.confirm("You have unsaved data. Are you sure?")
.pipe(first())
.subscribe((confirmed: boolean) => {
canDeactivate = confirmed;
});
} else {
canDeactivate = true;
}
return canDeactivate;
}
}