在Angular中,我有一項服務通過constructor(...)
注入了很少的東西。然而,該服務也是通過調用構造函數創建的。因此,添加其他服務取決於參數會改變API。我想避免這種情況。Angular - 以構造函數以外的方式注入?
有沒有辦法將服務注入到另一個服務而不將它添加到構造函數參數中?例如。現場注射?
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;
請提供https://stackoverflow.com/help/mcve。目前還不清楚你的案例是什麼,解決方案取決於實際的代碼。 – estus
請添加演示您嘗試完成的代碼。你的解釋很模糊。 –
根據您的更新,簡短答案是:否,Angular的DI本身只支持構造函數注入。 – jonrsharpe