Angularでは、私はconstructor(...)
を通して注入されるものがほとんどないサービスを持っています。しかし、そのサービスはコンストラクタを呼び出すことによっていくつかの場所にも作成されます。したがって、パラメータに依存する別のサービスを追加すると、APIが変更されます。私はそれを避けたいです。角度 - コンストラクタとは別の方法で注入するか?
サービスをコンストラクタパラメータに追加せずに別のサービスに挿入する方法はありますか?例えば。フィールドインジェクション?
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