Angular 依赖注册令牌类型

huangapple go评论116阅读模式
英文:

Angular dependency registration token type

问题

在下面的示例中,我想知道如何使用类型标记(type-token)来注册依赖项,而不是使用字符串标记(string-token),因为依赖关系是由一个类来表示的?在下面的示例中,service 可以轻松地通过useClass配置选项从一个类替换为另一个类(HeroService <-> NewheroService)。

如何重写这个示例,使得HeroListComponent构造函数的参数看起来像constructor( private service: MyService)?我尝试创建一个接口类MyService,其中HeroService和NewheroService继承自它。还将提供程序从'MyService'更改为MyService,但它会引发错误:

'MyService'只引用了一个类型,但在这里被用作值。

@Injectable()
export class HeroService {}

@Injectable()
export class NewheroService {}

hero.component.ts:

@Component({ ..., 
  providers: [{
    provide: 'MyService',
    useClass: HeroService
  }]
})
export class HeroListComponent {
  constructor(@Inject('MyService') private service: any) { }
}

如何重写这个示例,使得HeroListComponent构造函数的参数看起来像constructor( private service: MyService)

我尝试创建一个接口类MyService,其中HeroService和NewheroService继承自它。还将提供程序从'MyService'更改为MyService,但它会引发错误:

'MyService'只引用了一个类型,但在这里被用作值。

英文:

Instead of using string-token in the example below, I want to know how to use type-token to register the dependency, because the dependency is represented by a class? In the following example, service can easily be substituted through useClass configuration option from one class to another (HeroService <-> NewheroService).

How to rewrite this example that the parameter of HeroListComponent constructor looks like constructor( private service: MyService)? I tried to create interface class MyService, where HeroService and NewheroService inherits from. Also changed provider from &#39;MyService&#39; to MyService but it throws an error:

> 'MyService' only refers to a type, but is being used as a value here.

@Injectable()
export class HeroService {}

@Injectable()
export class NewheroService {}

hero.component.ts:

@Component({ ..., 
  providers: [{
    provide: &#39;MyService&#39;,
    useClass: HeroService
  }]
})
export class HeroListComponent {
  constructor(@Inject(&#39;MyService&#39;) private service: any) { }
}

答案1

得分: 1

这是关于注入令牌的主要用法:如果你曾经使用过Angular Material的对话框 (MAT_DIALOG_DATA),那么你已经在过去使用过它们了。

const MY_SERVICE = new InjectionToken<string>('某个唯一标识符');

@Component({ ..., 
  providers: [{
    provide: MY_SERVICE,
    useClass: HeroService
  }]
})
export class HeroListComponent {
  constructor(@Inject(MY_SERVICE) private service: any) { }
}
英文:

That's the main use of injection tokens : you have already used them in the past if you have used angular material's dialog (MAT_DIALOG_DATA)

const MY_SERVICE = new InjectionToken&lt;string&gt;(&#39;Some unique identifier&#39;);

@Component({ ..., 
  providers: [{
    provide: MY_SERVICE,
    useClass: HeroService
  }]
})
export class HeroListComponent {
  constructor(@Inject(MY_SERVICE) private service: any) { }
}

答案2

得分: 1

你可以通过创建一个通用服务来实现。

export class GeneralService {
  // 可以在这里定义通用方法或属性
}

然后你的服务可以继承它:

@Injectable()
export class HeroService extends GeneralService {
 constructor(){super();}
}

@Injectable()
export class NewheroService extends GeneralService {
 constructor(){super();}
}

然后在你的组件中可以像这样注入它:

@Component({ ..., 
  providers: [{
    provide: GeneralService,
    useClass: HeroService
  }]
})
export class HeroListComponent {
  constructor(private service: GeneralService) { }
}

GeneralService 可以注入到其他组件或服务中,Angular 的依赖注入会在使用接口的地方提供 HeroService 的实例。

Stackblitz: https://stackblitz.com/edit/angular-service-injection-e7s4k2?file=src%2Fapp%2Fhero.service.ts

英文:

You can do it by creating a general service.

export class GeneralService {
  //  Can be defined common methods or properties here
}

Then your services extends it:

@Injectable()
export class HeroService extends GeneralService {
 constructor(){super();}
}

@Injectable()
export class NewheroService extends GeneralService {
 constructor(){super();}
}

Then in your component you can inject it like this:

@Component({ ..., 
  providers: [{
    provide: GeneralService,
    useClass: HeroService
  }]
})
export class HeroListComponent {
  constructor(private service: GeneralService) { }
}

The GeneralService can be injected into other components or services, and Angular's dependency injection will provide the instance of HeroService wherever the interface is used.

Stackblitz: https://stackblitz.com/edit/angular-service-injection-e7s4k2?file=src%2Fapp%2Fhero.service.ts

huangapple
  • 本文由 发表于 2023年6月22日 13:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定