为什么要在DI中使用`deps`属性 [英] Why use `deps` property in DI
问题描述
这是 angular.io 的代码段:
{ provide: RUNNERS_UP, useFactory: runnersUpFactory(2), deps: [Hero, HeroService] }
...
export function runnersUpFactory(take: number) {
return (winner: Hero, heroService: HeroService): string => {
/* ... */
};
};
我的问题是为什么在这里使用deps
属性?使用deps
的一般情况是什么?
My question is why deps
property is used here? What are the general cases for using deps
?
推荐答案
这是一种告诉Angular依赖项注入需要注入到runnersUpFactory
返回的工厂函数中的依赖项的方法.
This is a way to tell Angular dependency injections what dependencies it needs to inject to the factory function returned by runnersUpFactory
.
对于服务,有一个@Injectable()
类告诉DI它需要分析该类的构造函数参数(与@Component()
,@Directive()
和@Pipe()
相同),但这似乎不适用于功能.因此,他们引入了deps
参数.
For services there is the @Injectable()
class to tell DI that it needs to analyze the constructor parameter of this class (same for @Component()
, @Directive()
, and @Pipe()
), but this seems not to work for functions. Therefore they introduced the deps
parameter.
DI将使用键Hero
查找提供者,并使用HeroService
查找另一个提供者,然后将它们作为参数以相同顺序传递给工厂函数.
DI will look up a provider using the key Hero
and another one using HeroService
and then will pass them as parameters to the factory function in the same order.
https://angular.io/docs/ts/latest/api/core/index/FactoryProvider-interface.html
deps : any[]
注入器需要解析的令牌列表.然后将值列表用作useFactory
函数的参数.
deps : any[]
A list of tokens which need to be resolved by the injector. The list of values is than used as arguments to theuseFactory
function.
这篇关于为什么要在DI中使用`deps`属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!