Angular 2 在构造函数之外注入依赖 [英] Angular 2 Inject Dependency outside Constructor

查看:26
本文介绍了Angular 2 在构造函数之外注入依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Angular 2 中深入研究 DI.我正在实现一个 REST 客户端,使用具体数据类型的通用子类型,如下所示:

I am currently digging into DI in Angular 2. I'm implementing a REST-Client using a generic subtypes for concrete Datatypes like this:

class RESTClient<T>{
    constructor() {
        var inj =  ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
        this.http = inj.get(Http);
        this.conf = RESTConfiguration;
    }
}
class BookClient extends RESTClient<Book>{      
    constructor(){
        // since I dont want to inject the HTTP Providers here, I'm using a custom    injector in the super class
        super();
    }
}

class WriterClient extends RESTClient<Writer>{      
    ...
    ...
}

据我所知,超类 REST-Service 注入的所有 RESTClient 之间将共享一个 http 服务.

So as I understand, there will be one http service shared between all RESTClients injected by the superclasses REST-Service.

现在我想要一个 RESTConfiguration 类:

Now I want to have a RESTConfiguration class as such:

@Injectable()
export class RESTConfiguration {
    get baseURL() {
     return this._baseURL;
    }

    set baseURL(value) {
        alert("sets value to"+value);
        this._baseURL = value;
    }

    private _baseURL;

}

它应该在主应用程序中配置如下:

It should be configured in the main app as such:

initializeApp(){
  this.restconf.baseURL = "http://localhost:3004/";
}
bootstrap(MyApp, [RESTConfiguration]).then();

我现在想知道如何将我的 RESTConfiguration 的一个单例实例注入到 RESTService 类中,而不将它传递给我希望保持无参数的构造函数,以减少代码重复并避免打字稿中的泛型问题.

I'm now wondering how to inject one singleton instance of my RESTConfiguration into the RESTService class without passing it to the constructor which I want to remain argument-less in order to reduce code duplication and avoid issues with generics in typescript.

在上面的示例(第一个代码片段)中,我尝试使用我创建的 ReflectiveInjector 注入我的配置,它为我提供了我的配置的自定义实例.

In the above example (first code snippet) I'm trying to inject my configuration using the ReflectiveInjector I created which delivers me a custom instance of my Configuration.

我想了几个解决办法:

  1. 通过使用服务或某些静态类属性使应用程序可用来访问应用程序全局注入器"

  1. Getting access to the Apps "global injector" by making one available using a service or some static class property

在我的配置中实现额外的单例逻辑

Implementing extra singleton-logic into my configuration

想办法在构造函数之外使用angular-native注入方法吗?

finding a way to use the angular-native injection method outside of the constructor?

我的想法是否有错误,或者我是否滥用了 DI 框架?

Are there mistakes in my thinking or am I misusing the DI framework ?

推荐答案

这应该为这个问题提供一个解决方案,而且在需要注入服务而不将其作为构造函数参数提供的任何情况下也有帮助.

This should provide a solution for this issue but also help in any case where one needs to inject a service without supplying it as a constructor parameter.

我在另一篇文章中看到了这个答案:存储用于组件的注入器实例

I saw this answer in another post: Storing injector instance for use in components

您可以在 AppModule 类中配置 Angular Injector,然后在任何其他类中使用它(您可以从任何类访问 AppModule 的成员).

You can configure the Angular Injector in your AppModule class, and then use it in any other class (you can access AppModule's members from any class).

在 AppModule 中添加:

In AppModule add:

export class AppModule { 
  /**
     * Allows for retrieving singletons using `AppModule.injector.get(MyService)`
     * This is good to prevent injecting the service as constructor parameter.
     */
    static injector: Injector;
    constructor(injector: Injector) {
        AppModule.injector = injector;
    }
}

然后在您的其他课程中,您可以执行以下操作(对于此问题,请将 MyService 替换为 Http):

Then in your other class you can do the following (for this question replace MyService with Http):

@Injectable()
export class MyClass{
    private myService;

    constructor(){
        this.myService = AppModule.injector.get(MyService);
    }
}

这相当于使用:

constructor(private myService: MyService){}

这篇关于Angular 2 在构造函数之外注入依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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