扩展Http的定制服务不会被注入 [英] Custom service in extended Http not getting injected

查看:186
本文介绍了扩展Http的定制服务不会被注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to extend the Http provider to intercept all requests that deliver a 403 status to handle an automatic logout. My custom InterceptingHttp should be declared as Http provider, so I don't need to care about a "special" http provider.

我必须遵循:

我的自定义Http提供者

My custom Http provider

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提供者的声明

The declaration as regular Http provider in NgModule

    @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 未定义。

It gets loaded and it does intercept all 403 responses. The only odd thing is, that the authenticationService is undefined.

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

I guess I'm probably do a mistake in my providers declaration. I tried to add AuthenticationService to the deps array, which only resulted into an Circular dependency error.

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

Where is my mistake? How can I use my AuthenticationService in my extended Http provider?

推荐答案

您需要将 AuthenticationService 添加到 deps

deps: [XHRBackend, RequestOptions, AuthenticationService]

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

When Http is injected to AuthenticationService this will cause a circular dependency that DI can't resolve.

请参阅 DI与循环依赖关系与自定义HTTP和ConfigService 有关如何解决循环依赖关系的方法。

See DI with cyclic dependency with custom HTTP and ConfigService for an approach how to work around circular dependency.

这篇关于扩展Http的定制服务不会被注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆