在守卫服务中使用路由参数

Use route parameters in guard service

我正在尝试检查用户是否可以根据路由访问某个组件。 所以我需要在守卫服务中获取一个路由参数。 我在其他主题中读到,当您在加载目标页面之前需要路由信息时,您需要 ActivatedRouteSnapshot

那(仍然)正确吗?当我尝试使用这种方法时,我得到了“没有 ActivatedRouteSnapshot 的提供者!”错误,这让我回到根据 this post

使用 ActivatedRoute
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
import { tap } from 'rxjs/operators';
import { EventManagerService } from './services/database/event/eventmanager.service';

@Injectable()
export class ManagerGuard implements CanActivate {

  constructor(private router: Router, private eventManagerService: EventManagerService, private route: ActivatedRouteSnapshot) {
  }

  canActivate() {
    const eventId = this.route.paramMap.get('eventId');
    return this.eventManagerService.getManagerProfile(eventId)
      .pipe(
        tap(authenticated => {
          if (!authenticated) {
            this.router.navigate(['manager']);
          }
        }),
      );
  }
}

我的错误是我在构造函数中使用了路由和状态,而它已经在 canActivate 接口中提供

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { tap } from 'rxjs/operators';
import { EventManagerService } from './services/database/event/eventmanager.service';

@Injectable()
export class ManagerGuard implements CanActivate {

  constructor(private router: Router, private eventManagerService: EventManagerService,) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {