小编lal*_*ana的帖子

我可以使用 Nestjs 注入接口而不是类吗?

我想用这样的东西:

猫接口.ts

export interface ICats {
  meow(): void
}
Run Code Online (Sandbox Code Playgroud)

猫服务.ts

@Injectable()
export class CatsService implements ICats {
  constructor()

  meow(): void {
    return;
  }
}
Run Code Online (Sandbox Code Playgroud)

猫模块.ts

@Module({
  providers: [CatsService]
  exports: [CatsService]
})
export class CatsModule {}
Run Code Online (Sandbox Code Playgroud)

动物.service.ts

@Injectable()
export class AnimalsService {
  constructor(@Inject('ICats') private readonly cats: ICats)

  test(): void {
    return this.cats.meow();
  }
}
Run Code Online (Sandbox Code Playgroud)

动物模块.ts

@Module({
  imports: [CatsModule],
  providers: [AnimalsService],
  exports: [AnimalsService]
Run Code Online (Sandbox Code Playgroud)

但收到 Nest can't resolve dependencies of the

dependency-injection typescript nestjs

2
推荐指数
1
解决办法
1955
查看次数

标签 统计

dependency-injection ×1

nestjs ×1

typescript ×1