扩展Http中的自定义服务没有被注入

时间:2016-12-19 15:11:06

标签: angular dependency-injection angular2-http

我想扩展Http提供程序以拦截所有提供403状态以处理自动注销的请求。 我的自定义InterceptingHttp应该被声明为Http提供商,因此我不需要关心"特殊" http提供商。

我必须遵循:

我的自定义Http提供程序

import { Injectable } from '@angular/core';
import { Request, XHRBackend, RequestOptions, Response, Http, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { AuthenticationService } from './../services/authentication.service';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

@Injectable()
export class InterceptingHttpService extends Http {

    constructor(backend: XHRBackend, defaultOptions: RequestOptions, private authenticationService: AuthenticationService) {
    super(backend, defaultOptions);
    }

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    var self = this;
    return super.request(url, options).catch((res: Response) => {
        if (res.status === 403) {
        console.log('intercepted');
        self.authenticationService.logout();
        }
        return Observable.throw(res);
    });
    }
}

声明为NgModule中的常规Http提供程序

    @NgModule({
    imports: [
        BrowserModule,
        ToastyModule.forRoot(),
        HttpModule,
    FormsModule,
        routing,
        Ng2BootstrapModule,
    PaginationModule
    ],

    declarations: [
        AppComponent,
        NavHeaderComponent,
        FooterCopyrightComponent,
        InventoryRootComponent,
        InventoryTreeComponent,
        InventoryDetailComponent,
        SetModelComponent,
        MetadataListComponent,
        MetadataDetailComponent,
        ScriptGeneratorComponent,
        SetDetailComponent,
        SetVersionComponent,
        SetContainerTreeComponent,
        SetContainerDetailComponent,
        FilterByPropertyPipe,
        OrderContainersByLeafPipe,
        ResolveStateId,
        ConfirmComponent,
        FocusDirective
    ],
    providers: [
    SetService,
        SetTypeService,
        SetContainerService,
        StateService,
        NotificationService,
        MetadataService,
        MetadataTypeService,
        EntityService,
        SetContainerMetadataService,
    AuthenticationService,
    InterceptingHttpService,
        ConfirmService,
        SiteVersionService,
    {
        provide: Http,
        useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, authenticationService: AuthenticationService) => {
        return new InterceptingHttpService(backend, defaultOptions, authenticationService);
        },
        deps: [XHRBackend, RequestOptions, AuthenticationService]
    },
    ],
    bootstrap: [AppComponent]
})

它被加载并且它确实拦截了所有403个响应。唯一奇怪的是,authenticationService未定义。

我想我的提供者声明可能会犯错误。我尝试将AuthenticationService添加到deps数组中,这只会导致循环依赖性错误。

我的错误在哪里?如何在扩展的Http提供程序中使用我的AuthenticationService

1 个答案:

答案 0 :(得分:1)

您需要将AuthenticationService添加到deps

deps: [XHRBackend, RequestOptions, AuthenticationService]

Http注入AuthenticationService时,这将导致DI无法解决的循环依赖。

有关如何解决循环依赖的方法,请参阅DI with cyclic dependency with custom HTTP and ConfigService