Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 获取当前管线组件实例或保护_Angular_Angular2 Routing - Fatal编程技术网

Angular 获取当前管线组件实例或保护

Angular 获取当前管线组件实例或保护,angular,angular2-routing,Angular,Angular2 Routing,我有一个导航区,在路由器出口旁边有一个注销按钮 app.component.html: login.service.ts 当用户按下注销按钮时,我希望在我的注销方法实际向服务器发送注销命令之前执行脏检查(允许用户取消注销) 我已尝试将路由器和激活的路由注入我的导航标头组件,但在这两种情况下,rootConfig属性均为null,阻止我访问canDeactivate 因此,我的问题是:如何访问当前路由的canDeactivate guard或当前路由组件本身的实例?canDeactivate类采用

我有一个导航区,在路由器出口旁边有一个注销按钮

app.component.html:

login.service.ts

当用户按下注销按钮时,我希望在我的注销方法实际向服务器发送注销命令之前执行脏检查(允许用户取消注销)

我已尝试将
路由器
激活的路由
注入我的导航标头组件,但在这两种情况下,rootConfig属性均为null,阻止我访问canDeactivate


因此,我的问题是:如何访问当前路由的canDeactivate guard或当前路由组件本身的实例?

canDeactivate类采用它正在检查的组件的类型。通过为具有canDeactivate方法的组件定义接口,您可以检查该方法是否存在,并在活动组件上有条件地调用它(如果它实现了该方法)。否则,您可以返回true并允许停用组件

接口组件停用扩展组件{
canDeactivate:()=>可观察的布尔值;
}
导出类CanDeactivateGuard实现CanDeactivate{
公共canDeactivate(当前组件:ICanComponentDeactivate,
路由:ActivatedRouteSnapshot,
状态:RouterStateSnapshot):可观察的|布尔值{
返回currentComponent.canDeactivate?currentComponent.canDeactivate():true;
}
}

对于任何有类似问题的人:虽然这不是我最初问题的真正解决方案(获取当前路线的实例或访问他们的警卫手册),但我可以通过更改顺序并利用路由器返回的承诺来实现我的目标。导航(…):


对不起,我误解了你的问题。为什么您认为需要访问
canDeactivate
?为什么不让路由器执行它,然后返回
false
,以防用户单击取消?是否可以使注销成为自己的路由?然后canDeactivate将自动执行。据我所知,这是路由和导航教程中的实现,我就是这样做的。当用户手动导航离开某个页面时,卫士的工作方式为exprected。我编辑了我的问题,试图澄清它。我不理解你的问题。您能告诉我们您正在尝试做的哪些工作不起作用,或者可以共享一些代码吗?
<my-nav-header></my-nav-header>
<router-outlet></router-outlet>
logoutBtnClick(){
    // todo: insert call to canDeactivate here
    // or find another way to get dirty state of current component in router
    //console.debug('logout - can deactivate[1]: ', this.activatedRoute.routeConfig.canDeactivate);
    //console.debug('logout - can deactivate[2]: ', this.activatedRoute.snapshot.routeConfig.canDeactivate);
    this.loginSvc.doLogout();
}
@Injectable()
export class LoginService {

...

  public doLogout(){
    this.myHttp.sendLogout().subscribe(
        ()=> {
            this.clearSessionData();
            this.myHttp.clearSessionData();
            this.router.navigate(['/login']); // triggering canDeactivate, but too late as logout command already has been sent to the server
  }
@Injectable()
export class LoginService {

...

  public doLogout(){
    this.router.navigate(['/login'])
      .then((routerNavigationResult: boolean)=> {
        if (routerNavigationResult) {
          // only when the navigation is allowed, i.e. no change or change disccarded
          this.myHttp.sendLogout().subscribe(
            (data) => this.clearSessionData(),
            (error) => this.handleLogoutError(error) // in my case also clears session data
          );
        }
      });
  }