2017-04-14 79 views
0

我写一个http intercpetor,它是这样的:angular2 http拦截器和注入SlimLoadingBarService不起作用?

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拦截器是工人,但SlimLoadingBarService doew不行。

我觉得应该是错的,通过new SlimLoadingBarService的领先,但直接传输SlimLoadingBarService,同样会给出,现在卡在这个地方不知道该怎么继续。

没有错误信息,但加载栏(SlimLoadingBarService)没有显示。

+1

你的错误是什么? –

+0

这个问题是相当神秘的,请详细说明它,以获得一些帮助,描述你的问题,遇到的错误等。“不工作”不是一个问题的描述。 – n00dl3

+0

@ Karbos538这不是错误消息,但加载栏不显示。 –

回答

2

试试这个。

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

谢谢提醒,这里确实有问题,但主要原因是我的http拦截器调用问题已经解决。 –