在Augular中将2个Guards用于1条路由不起作用

时间:2019-02-27 22:34:46

标签: angular7 angular-router angular-guards

我创建了3个不同的后卫: 学生卫队 教师守卫 AdminsGuard

我希望Admins Guard和Student Guard都允许访问学生组件,但由于某种原因AdminGuard无法访问学生组件。

我的守卫:

// StudentGuard:

class MenuView: UIView {

// MARK: - Outlets
@IBOutlet weak var chatButton: UIButton!
@IBOutlet weak var loginButton: UIButton!
@IBOutlet weak var animationButton: UIButton!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    insertSubview(backgroundImage, at: 0)
    backgroundImage.topAnchor.constraint(equalTo: topAnchor).isActive           = true
    backgroundImage.leadingAnchor.constraint(equalTo: leadingAnchor).isActive   = true
    backgroundImage.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
    backgroundImage.bottomAnchor.constraint(equalTo: bottomAnchor).isActive     = true

    chatButton.layer.cornerRadius = 8
    loginButton.layer.cornerRadius = 8
    animationButton.layer.cornerRadius = 8
}

// MARK: - BackgroundImage
private var backgroundImage: UIImageView  = {
    let imageView = UIImageView(image: UIImage(named: "bg_home_menu"))
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()
}

// TeachersGuard:

 export class StudentAuthGuard implements CanActivateChild {
  constructor(private sauthService: StudentAuthService, private alertify: AlertifyService,
      private router: Router) {}
  canActivateChild(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot) {
    if (this.sauthService.isStudentLoggedIn()) {
      return true;
    } else {
      this.router.navigate(['/home']);
      this.alertify.error('Access Denied');
    }
  }
}

// AdminGuard

  export class TeacherAuthGuard implements CanActivateChild {
  constructor(private sauthService: StudentAuthService, private alertify: AlertifyService,
    private router: Router) {}
canActivateChild(
  next: ActivatedRouteSnapshot,
  state: RouterStateSnapshot) {
  if (this.sauthService.isTeacherLoggedIn()) {
    return true;
  } else {
    this.router.navigate(['/home']);
    this.alertify.error('Access Denied');
  }
}
}

//路由模块:

 export class AdminAuthGuard implements CanActivateChild {
  constructor(private sauthService: StudentAuthService, private alertify: AlertifyService,
    private router: Router) {}
canActivateChild(
  next: ActivatedRouteSnapshot,
  state: RouterStateSnapshot) {
  if (this.sauthService.isAdminLoggedIn()) {
    return true;
  } else {
    this.router.navigate(['/home']);
    this.alertify.error('Access Denied');
  }
}
}

请帮助我无法理解问题所在

0 个答案:

没有答案