简体   繁体   English

动态 canDeactivate Guard:在离开带有更改的表单之前显示确认消息

[英]Dynamic canDeactivate Guard: Show confirm message before navigating away from form with changes

I'm working on a project with lot of forms pages, I want to give intimation to the end-user whenever they try to navigate to another route without saving their changes.我正在处理一个包含大量表单页面的项目,我想在最终用户尝试导航到另一条路线而不保存更改时向他们提供提示。 In all the pages I'm using reactive forms something like this在所有页面中,我都使用类似这样的反应形式

this.mainForm = this.formBuilder.group(...

So can I have one can deactivate Guard for all my components something like this那么我可以让我的所有组件都停用Guard 吗?

@Injectable()
class CanDeactivateTeam implements CanDeactivate<... something magic here want to pass component dynamically...> {

constructor() {}

canDeactivate(
    component: ...dynamicComponent,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState: RouterStateSnapshot
    ): Observable<boolean|UrlTree>|Promise<boolean|UrlTree>|boolean|UrlTree {
        if(!this.mainForm.dirty) return true;
    }
}

is it possible to have the same can deactivate guard for all the pages to prevent form changes?是否可以对所有页面使用相同的可以停用保护以防止表单更改?

Thanks in advance.提前致谢。

You will need form base component.您将需要表单基础组件。 Base component will have function to check if form is dirty and return a promise which resolves to boolean.基础组件将具有检查表单是否脏并返回解析为布尔值的承诺的功能。

In your CanDeactivateGuard you can call this function.在您的 CanDeactivateGuard 中,您可以调用此函数。 Now you just need inherit all form components from this base component and update routes set canDeactivate.现在您只需要从这个基本组件继承所有表单组件并更新路由集 canDeactivate。

//CanDeactivateGuard.ts

export class CanDeactivateGuard implements CanDeactivate<AdminFormBaseComponent> {
     canDeactivate(component: FormBaseComponent): Observable<boolean> | boolean 
     {
        return component.verifyChangesAndConfirm();
     }
}


//FormBaseComponent.ts
export class FormBaseComponent{
   @ViewChild('form', { static: true }) form;

   public verifyChangesAndConfirm(): Observable<boolean> | boolean {
      if (this.form && !this.form.submitted && this.form.dirty) {
         var subject = new Subject<boolean>();
         this._modalService.ShowConfirmDialog(() => {
            //yes
            subject.next(true);
         }, () => {
            //no
            subject.next(false);
         });
         return subject.asObservable();
     }
     return true;
   }
}

//SomeDataEntryFormComponent.ts
export class SomeDataEntryFormComponent extends FormBaseComponent implements 
OnInit {
   //Other code
}

And there should be following entry in route configuration并且在路由配置中应该有以下条目

{ path: 'some-data-entry-form', component: SomeDataEntryFormComponent, canDeactivate: 
[CanDeactivateGuard] },

You will have to implement modal service and inject so that _modalService.ShowConfirmDialog works.您将必须实现模态服务并注入,以便 _modalService.ShowConfirmDialog 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM