等待CanDeactivate方法以获取对话框响应

时间:2019-09-17 10:28:58

标签: angular8

我想保留“ canDeactivate Guard”方法,直到我的组件调用对话框服务,并且该对话框服务将响应返回给组件,然后我才想将响应提供给“ canDeactivate Guard方法”。

import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { ConfigurationComponent } from 'src/app/components/configuration/configuration.component';

@Injectable({
  providedIn: 'root'
})
export class DeactivateGuard implements CanDeactivate<any>
{
  component: Object;
  route: ActivatedRouteSnapshot;

  constructor(){
  }

  canDeactivate(component:ConfigurationComponent, route: ActivatedRouteSnapshot, state: RouterStateSnapshot, nextState: RouterStateSnapshot) : Observable<boolean> | Promise<boolean> | boolean {
    return component.onExitConfigComponent() ? component.onExitConfigComponent() : false;
  };
};

//This is component method code

onExitConfigComponent() {
      if (this.addNewAddonForm) {
        this.modal.openConfigurationPayrollModal({type: 'Discard New Add-on', MsgHeader: 'Discard new add-on', MsgDesc: 'Do you want to Discard New Add-on?'});
        this.modal.DialogResult.pipe(takeUntil(this.modalSubscribe$)).subscribe(val => {
          if (val == 'Discard New Add-on') {
           //do some logic
            return true;
          }
          else {
            //do some logic then return
              return false;
            }
          }
        });
      }
      return true
  };

0 个答案:

没有答案