5

我正在尝试实施授权保护https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-child-guard

但我得到一个 DI 错误,我真的不知道为什么我UserService在我的模块中注入我的。

RouteGuard在全局模块中声明,我UserService也是

  @NgModule({
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  imports: [
   TranslateModule
  ],
  exports: [
    LoadingComponent,
         ],
  declarations: [
    LoadingComponent,

  ],
  entryComponents: [],
  providers: [UserService,RouteGuard]
})
export class GlobalModule { }

错误:

   Uncaught SyntaxError {__zone_symbol__error: Error: Can't resolve all parameters for RouteGuard: ([object Object], ?).
    at SyntaxError.ZoneAwa…, _nativeError: ZoneAwareError}

代码

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

@Injectable()
export class RouteGuard extends ParentClass implements CanActivate
{
  constructor(
    private router: Router,
    private userService: UserService
  )
  {
    super();
  }

  public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> 
  {
    debugger;

    return this.userService.getUser(this.subId()).toPromise().then(
      (data) =>
      {
        this.setLoggedUser(data);
        if (!this.isAdministrator())
        {
          this.router.navigate(['']);
          return false;
        }
        return true;

      }).catch((err) => { this.error(err); });

  }
}
4

0 回答 0