为什么要在DI中使用`deps`属性 [英] Why use `deps` property in DI

查看:112
本文介绍了为什么要在DI中使用`deps`属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 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 the useFactory function.

这篇关于为什么要在DI中使用`deps`属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆