我写了一个http intercpetor,它是这样的:

import { Injectable, Inject } from '@angular/core';
import { SlimLoadingBarService } from 'ng2-slim-loading-bar';
// ... other imports in here

@Injectable()
export class HttpInterceptorService extends Http {
    private apiEndpoint: string;

    constructor(
        @Inject(CONFIG_TOKEN) config: Config,
        private backend: ConnectionBackend,
        private defaultOptions: RequestOptions,
        private slimLoadingBarService: SlimLoadingBarService
    ) {
        super(backend, defaultOptions);
        this.apiEndpoint = config.apiEndpoint;
    }

    get(url: string, options?: RequestOptionsArgs): Observable<any> {
        this.beforeRequest();
        return super.get(this.getFullUrl(url), this.requestOptions(options))
            // ...
    }

    private beforeRequest(): void {
        // this is not work
        this.slimLoadingBarService.start();
    }

    // ... other methods
}


我的app.module提供这样的配置:

{
    provide: HttpInterceptorService,
    useFactory: (backend: XHRBackend, defaultOptions: RequestOptions) => {
        return new HttpInterceptorService(CONFIG, backend,
            defaultOptions, new SlimLoadingBarService);
    },
    deps: [XHRBackend, RequestOptions]
}


现在,这个Http拦截器是worker,但是SlimLoadingBarService不能正常工作。

我觉得传递new SlimLoadingBarService引线应该是错误的,但是直接传递SlimLoadingBarService时,会给出同样的结果,现在卡在这个地方不知道如何继续。

没有错误消息,但是loadingbar(SlimLoadingBarService)没有显示。

最佳答案

试试这个吧。

{
  provide: HttpInterceptorService,
  useFactory: (backend: XHRBackend, defaultOptions: RequestOptions, slimLoadingBarService: SlimLoadingBarService) => {
    return new HttpInterceptorService(CONFIG, backend,
        defaultOptions, slimLoadingBarService);
  },
  deps: [XHRBackend, RequestOptions, SlimLoadingBarService]
}

关于javascript - angular2 http拦截器并注入(inject)SlimLoadingBarService不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43409060/

10-09 15:04