2017-04-14 80 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攔截器調用問題已經解決。 –