0

例如,以下伪代码可以以某种方式工作吗?是否有某种注入令牌是被注入的类?


function AnimalSoundServiceFactory(classBeingInjectedInto) {

  if (classBeingInjectedInto is a Dog) { return DogSoundService; }
  if (classBeingInjectedInto is a Cat) { return CatSoundService; }
  if (classBeingInjectedInto is a Cow) { return CowSoundService; }

}

...

{ provide: AnimalSoundService, useFactory: AnimalSoundServiceFactory }

...


class Dog {
  constructor(soundService: AnimalSoundService) {
    console.log(soundService.getMySound();
  }
}
4

0 回答 0