Skip to content

动态注入

在运行时需要动态访问单例提供者(service)可以借助核心类:ModuleRef

moduleRef.get(service)

例如下面的业务代码:

typescript
export class IsAuthorGuard implements CanActivate {
  constructor(
    private readonly module: ModuleRef,
    private readonly reflector: Reflector
  ) {}

  async canActivate(context: ExecutionContext) {
    const request = context
      .switchToHttp()
      .getRequest<Request<{ id: string }>>();

    const kindOf = this.reflector.get<Entities>(
      ENTITY_METADATA_KEY,
      context.getHandler()
    );

    const service = this.module.get<ArticleService | CommentService>(
      kindOf === Entities.COMMENT ? CommentService : ArticleService
    );

    // other logic
  }
}

读取上下文中的元数据,当 ENTITY_METADATA_KEY === Entities.COMMENT

下面的逻辑使用 CommentService 中提供的方法,否则使用 ArticleService 中提供的方法

2025( )
今日 20.83%
本周 42.86%
本月 10.00%
本年 67.40%
Powered by Snowinlu | Copyright © 2024- | MIT License