相关疑难解决方法(0)

Angular 2在构造函数外部注入依赖项

我正在深入研究Angular 2中的DI.我正在使用泛型子类型实现一个REST-Client,用于具体的数据类型,如下所示:

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>{      
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

现在我希望有一个RESTConfiguration类:

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

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

    private _baseURL;

}
Run Code Online (Sandbox Code Playgroud)

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

singleton dependency-injection typescript angular

14
推荐指数
1
解决办法
1万
查看次数