简体   繁体   English

理解angular2中的依赖注入

[英]Understanding Dependency injection in angular2

Here is a code snippet 这是一段代码片段

@Component({
     ... : ...
     providers: [MyService]
})
export class MyComponent{

     constructor(private _myService : MyService){
     }

     someFunction(){
          this._myService.getSomething();
     }
}

And Here is my understanding/question how it works in angular2/typescript. 这是我的理解/问题它在angular2 / typescript中是如何工作的。

  • A singleton object will be created whenever we write MyService in providers . 每当我们在providers编写MyService ,都会创建一个单例对象。
  • Why to give it in a constructor? 为什么要在构造函数中赋予它? Can we do it anywhere else? 我们可以在其他地方做到吗?
  • whoever calls the constructor (javascript engine maybe), how would it know what to pass in to the arguments 无论谁调用构造函数(也许是javascript引擎),它将如何知道传递给参数的内容
  • or I am getting it wrong, it is not even a argument? 或者我弄错了,甚至不是一个论点?

EDIT 编辑

  • what if i provide providers: [MyService] at two controller. 如果我在两个控制器上提供提供providers: [MyService]该怎么办? Will it create a new instance or fetch from one? 它会创建一个新实例还是从一个实例获取?
  • provider: [ ... ] specifies the scope of the single instance maintained for each provider. provider: [ ... ]指定为每个提供者维护的单个实例的范围。 The scope is the component where the type is added to the providers: [ ... ] and its descendants (as long as they don't override it with another provider with the same key). 范围是将类型添加到providers: [ ... ]的组件providers: [ ... ]及其后代(只要它们不使用具有相同键的另一个提供程序覆盖它)。 Listing a provider in bootstrap(AppComponent, [SomeProvider]) makes it globally available, again as long as not shadowed by a provider on a component. bootstrap(AppComponent, [SomeProvider])程序中列出提供bootstrap(AppComponent, [SomeProvider])使其全局可用,只要不被组件上的提供程序遮蔽。

  • Angular2 only supports contructor injections. Angular2仅支持构造函数注入。 There are other strategies but they are considered problematic. 还有其他策略,但它们被认为是有问题的。

  • Angular analyzes the contructor parameter to know what parameters need to be passed in when an instance is created, acquires appropriate instances from its providers or parent providers and passes them in. Angular分析contructor参数以了解创建实例时需要传递的参数,从其提供者或父提供者获取适当的实例并将其传递。
    For this to work classes need to be decorated with @Injectable() if they are not components, directives or pipes. 对于这个工作类,如果它们不是组件,指令或管道,则需要使用@Injectable()进行修饰。

  • DI (Dependency injection) only passes instances to constructor parameters for instances it created itself. DI(依赖注入)仅将实例传递给它自己创建的实例的构造函数参数。 For each instance it creates, it checks the constructor parameters and acquires the matching instances from itself (providers) and passes them in. If one of these dependencies itself again needs constructor parameters, this goes recursively until all dependencies are resolve. 对于它创建的每个实例,它检查构造函数参数并从自身(提供者)获取匹配的实例并将它们传入。如果其中一个依赖项本身再次需要构造函数参数,则会递归执行,直到所有依赖项都解析为止。
    Jasmine doesn't pass instances to the constructor itself, it has to delegate it to DI instead. Jasmine不会将实例传递给构造函数本身,它必须将它委托给DI。

Update to your "Edit" 更新到“编辑”

If the same service is added at different places bootstrap(...) , providers: [...] then a single instance is maintained per each provider. 如果在不同的地方添加相同的服务bootstrap(...)providers: [...]则每个提供者维护一个实例。

When a class requests a dependency (by a constructor parameter) the DI looks upwards towards bootstrap() and returns the instance of the first provider it finds. 当一个类请求依赖(通过构造函数参数)时,DI向上看向bootstrap()并返回它找到的第一个提供者的实例。 (this is why providers on components shadow providers of ancestor components or bootstrap(...) (这就是为什么组件的提供者影响祖先组件或bootstrap(...)提供者bootstrap(...)

Short: yes, two providers of the same service result in two different instance. 简短:是的,同一服务的两个提供者导致两个不同的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM