我正在尝试为XHRBackend
该类创建一个包装器,并且我已经能够在最外层成功地创建和使用所有提供的服务,AppComponent
但我无法将提供程序打包到实际服务本身中。我将类注入到组件构造函数中,但我无法想象这会破坏任何东西。
当前AppComponent
代码:
import {HTTP_PROVIDERS, Http, ConnectionBackend, RequestOptions} from "@angular/http";
import {ROUTER_DIRECTIVES} from "@angular/router";
import {XHRBackendWrapper} from 'backendwrapper.services';
@Component({
selector: 'app',
template: './app.component.html',
directives: [ROUTER_DIRECTIVES],
providers: [
HTTP_PROVIDERS,
provide(Http, {
useFactory: (xhrBackend: ConnectionBackend, requestOptions: RequestOptions) => new Http(xhrBackend, requestOptions),
deps: [XHRBackendWrapper, RequestOptions]
}),
XHRBackendWrapper
]
})
export class AppComponent {
constructor(xhrBackend: XHRBackendWrapper){
}
}
所需AppComponent
代码:
import {HTTP_PROVIDERS} from "@angular/http";
import {ROUTER_DIRECTIVES} from "@angular/router";
import {BACKEND_PROVIDERS, XHRBackendWrapper} from 'backendwrapper.services';
@Component({
selector: 'app',
template: './app.component.html',
directives: [ROUTER_DIRECTIVES],
providers: [
HTTP_PROVIDERS,
/** THIS IS THE ONLY DIFFERENCE HERE!!! ***/
BACKEND_PROVIDERS
]
})
export class AppComponent {
constructor(xhrBackend: XHRBackendWrapper){
}
}
从后端包装器中导出的所需常量:
export const BACKEND_PROVIDERS = [
provide(Http, {
useFactory: (xhrBackend: ConnectionBackend, requestOptions: RequestOptions) => new Http(xhrBackend, requestOptions),
deps: [XHRBackendWrapper, RequestOptions]
}),
XHRBackendWrapper
];
当我尝试这样做时One or more of providers for "AppComponent" were not defined
,我的控制台中出现一个错误,因为XHRBackendWrapper
找不到。我在这里想念什么?
我不能发布所有代码,因为它会使这个问题太大,但是,如果它有帮助,这是 XHRBackendWrapper 的一般想法:
@Injectable()
export class XHRBackendWrapper extends ConnectionBackend {
constructor(private _xhrBackend: XHRBackend)
{
super();
}
createConnection(request: Request)
{
return this._xhrBackend.createConnection(request);
}
}