2016-08-29 3 views
2

Aus irgendeinem Grund möchte ich nicht Konstruktor (private http: Http) DI verwenden.Wie erstellt man eine Http-Instanz ohne Konstruktor DI?

bei https://angular.io/docs/ts/latest/api/http/index/Http-class.html Sah

und versuchte

const injector = ReflectiveInjector.resolveAndCreate([ 
    BaseRequestOptions, 
    XHRConnection, 
    { 
    provide: Http, 
    useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions), 
    deps: [XHRConnection, BaseRequestOptions] 
    } 
]); 

this.http = injector.get(Http); 

Fehler sagt

ORIGINAL AUSNAHME: Alle Parameter für 'XHRConnection' nicht auflösen (?,?,?). Stellen Sie sicher, dass alle Parameter mit Inject versehen sind oder gültige Typ-Annotationen haben und dass "XHRConnection" mit Injectible dekoriert ist.

Antwort

1

Sie müssen HTTP_PROVIDERS bieten:

const injector = ReflectiveInjector.resolveAndCreate([ 
    HTTP_PROVIDERS, 
    XHRConnection, 
    { 
    provide: Http, 
    useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions), 
    deps: [XHRConnection, BaseRequestOptions] 
    } 
]); 
+0

HTTP_PROVIDERS ist jetzt veraltet. Hast du ein Beispiel für RC.5 +? –

0

Eigentlich kann es so einfach sein wie

const injector = ReflectiveInjector.resolveAndCreate([ 
    HTTP_PROVIDERS 
]); 

this.http = injector.get(Http); 

Basierend auf Günter Zöchbauer Antwort.

+0

HTTP_PROVIDERS ist jetzt veraltet. Hast du ein Beispiel für RC.5 +? –

Verwandte Themen