问题
In Angular, I have a service that has few things injected through the constructor(...)
. However that service is also at some place created by calling the constructor. Therefore, adding another service it depends on to the parameters would change the API. I'd like to avoid that.
Is there a way to inject a service into another service without adding it to the constructor parameters? E.g. field injection?
import {Inject, Injectable} from "@angular/core";
import {
Http, Request, ConnectionBackend, RequestOptions, RequestOptionsArgs, Response, Headers,
RequestMethod
} from "@angular/http";
import {KeycloakService} from "./keycloak.service";
import {Observable} from 'rxjs/Observable';
import {EventBusService} from "../events/event-bus.service";
import {LoadingSomethingFinishedEvent, LoadingSomethingStartedEvent} from "../events/windup-event";
@Injectable()
export class WindupHttpService extends Http {
constructor(
_backend: ConnectionBackend,
_defaultOptions: RequestOptions,
private _keycloakService: KeycloakService,
// ----- This is what I want to avoid. -----
private _eventBus: EventBusService,
) {
super(_backend, _defaultOptions);
}
// ------- This is what I am looking for ---------
//@Inject()
//private _eventBus: EventBusService;
回答1:
Yes and no.
You can use the Injector
, but the best way is to inject it into the service:
constructor(private injector: Injector) {
let httpService: Http = this.injector.get(Http);
}
More about Injector can be found here: https://angular.io/api/core/Injector
Here is also usable link, as @DBosley mentioned: https://angular.io/guide/dependency-injection#appendix-working-with-injectors-directly
回答2:
You can do this within Angular's DI using factory providers:
injectFields(dependency: SomeDependency) {
let service = new FieldInjectedService();
service.dependency = dependency;
return service;
}
...
providers: [
{ provide: FieldInjectedService, useFactory: injectFields, deps: [SomeDependency] },
...
]
Here the FieldInjectedService
doesn't have any constructor parameters, but must have its dependency
property set to be any use. The downside, of course, is that nothing here requires you to set those fields, so you could easily create an invalid instance of the service.
来源:https://stackoverflow.com/questions/45225676/angular-inject-in-other-way-than-constructor