反射式注入器与角度注入器之间的区别 [英] Difference between Reflective Injector and Injector in Angular

查看:60
本文介绍了反射式注入器与角度注入器之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用两种方式显式创建依赖关系.两者几乎相同,但是我有点困惑使用反射式喷射器比普通喷射器有什么优势,推荐哪种方式?

I tried to create dependency explictly using two ways. Both are almost same but I am little bit confusing what's the advantage of using Reflective Injector over the normal Injector and which one is recommended way?

使用进样器

import { Injector } from '@angular/core';
constructor( private injector: Injector) {
    this.myService = this.injector.get(MyService);
  }

使用反射式注入器

import { ReflectiveInjector } from '@angular/core';
 constructor() {

       var injector= ReflectiveInjector.resolveAndCreate([MyService]);
       let myservice=injector.get(MyService);
     }

推荐答案

注入程序是带有提供程序/服务的容器.它实现一个方法get,并返回服务的一个实例.让我们用JS伪代码实现注入器的最基本版本:

An injector is a container with providers/services. It implements one method get and returns an instance of the service. Let's implement the most basic version of the injector in JS pseudocode:

class ReflectiveInjector {
   providers = [];

   static resolveAndCreate(providers) {
     providers.forEach((provider)=>{
         providers.push({token: provider.provide, instance: new provider.useClass})
     });  
  )}

  get(dep) {
     return providers.find((provider)=>{ return provider.token === token });
  }
}

现在,我们需要先使用喷射器的实例,然后才能使用它.当我们创建它时,我们定义了提供者:

Now, we need to create the instance of the injector before we can use it. And when we create it we define providers:

const existingInjector = ReflectiveInjector.resolveAndCreate([{provide: A, useClass: A }]);
const AInstance = existingInjector.get(A);

因此,您看到要使用注射器,必须首先创建它.它没有允许添加提供者的任何特定方法,因此在创建之后,我们无法向其添加任何新的提供者.

So you see in order to use an injector it has to be created first. It doesn't have any specific methods that allow adding providers so after it's created we can't add any new providers to it.

您注入到组件构造器中的注入器已由Angular创建.您无法向其中添加任何内容,并且只能查询已经在其上定义的提供程序.如果需要提供B类,则无法使用现有的注射器进行.您需要一个新的.这就是ReflectiveInjector类的用处.它允许您通过创建注射器的新实例并注册新的提供程序来添加新的提供程序.好处是它还可以设置喷油器链.

The injector you inject into the component constructor is already created by Angular. You can't add anything to it and can query only the providers already defined on it. If you need to provide B class, you can't do that with existing injector. You need a new one. And this is where ReflectiveInjector class comes in. It allows you to add new providers by creating a new instance of the injector and registering new providers. And the good part is that it can also setup an injector chain.

让我们稍微修改一下resolveAndCreateget方法以允许链接注射器:

Let's a bit modify resolveAndCreate and get methods to allow chaining injectors:

class ReflectiveInjector {
   providers = [];
   parent;

   static resolveAndCreate(providers, parent) {
       this.parent = parent;
   ...
   }

   get(dep) {
       let found = providers.find((provider)=>{ return provider.token === token });
       if (!found && parent) {
            found = parent.get(dep);
       }

       return found;
   }

所以现在剩下的就是使用它:

So now the only that left is to use it:

// passing existingInjector as a parent
const childInjector = ReflectiveInjector.resolveAndCreate([{provide: B, useClass: B }], i);
const AInstance = childInjector.get(A);
const BInstance = childInjector.get(B);

现在,假设有人可能想要访问我们的existingInjector.我们需要一个令牌来获取这个现有的注射器.让我们这样定义此令牌:

Now, suppose someone might want to get access to our existingInjector. We need a token to use to get this existing injector. Let's define this token like this:

abstract class Injector {}

让我们写一个函数来获取现有的注射器:

and let's write a function that will get the existing injector:

function resolveDependency(token) {
   if (token === Injector) {
       return existingInjector;
   }
}

现在假设Angular在执行组件的构造函数时,使用您指定的标记获取依赖项并将它们传递给resolveDependency函数.所以你这样写:

And now suppose that Angular when executes the constructor of the component, uses the tokens you specified to get dependencies and passes them to the resolveDependency function. So you write like this:

// the Injector here is a reference to our abstract Injector class and simply used as a token
MyComp {
   constructor(private injector: Injector) { ...  }
}

这里的令牌是Injector,正如我所说的,该令牌传递到resolveDependency函数中,并返回现有的注射器.

And the token here is Injector which as I said is passed into the resolveDependency function and the existing injector is returned.

这篇关于反射式注入器与角度注入器之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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