简体   繁体   English

angular2 http拦截器并注入SlimLoadingBarService不起作用?

[英]angular2 http interceptor and inject SlimLoadingBarService does not work?

I write a http intercpetor,it is like this: 我写了一个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
}

My app.module provide config like this: 我的app.module提供这样的配置:

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

Now, this Http interceptor is worker,But the SlimLoadingBarService doew not work. 现在,这个Http拦截器是worker,但是SlimLoadingBarService却无法正常工作。

I feel should be wrong to pass the new SlimLoadingBarService lead, but the direct transmission SlimLoadingBarService , the same will be given, and now stuck in this place do not know how to continue. 我觉得传递new SlimLoadingBarService引线应该是错误的,但是直接传递SlimLoadingBarService ,将给出相同的结果,现在卡在这个地方不知道如何继续。

There is no error message, but loadingbar( SlimLoadingBarService ) does not show up. 没有错误消息,但是loadingbar( SlimLoadingBarService )没有显示。

Try this instead. 试试这个吧。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM