简体   繁体   English

导航到另一条路线后,能否继续在Angular中继续活动?

[英]Can CanActivate in Angular continue after navigating to another route?

In an auth guard when authentication is missing I want to display Facebook login. 在身份验证丢失的身份验证卫士中,我想显示Facebook登录名。 Wonder if it will work to do router.navigate(['login']) in canActivate to trigger authentication and return async result of the login or will it be interrupted by the router.navigate call. 不知道是否会工作做router.navigate(['login'])canActivate触发认证,并返回登录的异步结果还是会被打断router.navigate电话。 Wonder about proper and simple flow here. 在这里想知道适当和简单的流程。

yes it is possible. 对的,这是可能的。 you can also use navigateByUrl to get to facebook 您还可以使用navigationByUrl转到Facebook

import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { UserService } from './user.service';

@Injectable()
export class AuthGuardService implements CanActivate {

  constructor(private userService: UserService, private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    if (this.userService.isValid()) {
      return true;
    } else {
      this.router.navigate(['/login'], {
        queryParams: {
          return: state.url
        }
      });
      return false;
    }
  }
}

code snippet credits go to / more examples can be found on : http://gnomeontherun.com/2017/03/02/guards-and-login-redirects-in-angular/ 代码片段来源/更多示例可以在以下位置找到: http : //gnomeontherun.com/2017/03/02/guards-and-login-redirects-in-angular/

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

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