dzzzzzy / Nestjs-Learning

nestjs 学习教程 :books:,跟我一起学习 nest 框架~ :muscle:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

guard中怎么注入service?

silent-tan opened this issue · comments

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(
    @Inject(Reflector) private readonly reflector: Reflector,
    @Inject(UserRoleService) private readonly userRoleService: UserRoleService,
  ) {}
}

守卫没有module,所以对于如何注入service好迷茫....

我把这个守卫变成了全局的守卫也没有用

providers: [
    {
      provide: APP_GUARD,
      useClass: RolesGuard,
    },
  ]

把useClass方式,改成useFactory,就可以使用依赖注入, 文档

谢谢