0

我正在尝试为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);
    }
}
4

1 回答 1

0

不完全确定,但您可能需要像这样解构您的 BACKEND_PROVIDERS:

providers: [
    ...HTTP_PROVIDERS,
    /** THIS IS THE ONLY DIFFERENCE HERE!!! ***/
    ...BACKEND_PROVIDERS
]
于 2016-07-14T14:15:25.907 回答