2017-08-01 57 views
0

我有一個HTTP攔截器來監視401錯誤和超時錯誤。當我在裏面導入一個服務時。跟它的服務是不明確的HTTP攔截器內的模塊導入得到未定義

我的HTTP攔截

// dependencies 
import { Injectable } from '@angular/core'; 
import { ConnectionBackend, XHRBackend, RequestOptions, Request, RequestOptionsArgs, Response, Http, Headers } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 
import { environment } from '../../../environments/environment'; 
import { Router } from '@angular/router'; 

import { NotificationService } from '@sharedServices/notification/notification.service'; 

@Injectable() 
export class HttpInterceptor extends Http { 

    constructor(backend: XHRBackend, defaultOptions: RequestOptions, private _router: Router, private _notify: NotificationService) { 
    super(backend, defaultOptions); 
    } 

    request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { 
    return super.request(url, options) 
     .timeout(2000) // set timeout 15s for every request 
     .catch((error: Response) => { 
     if (error.status === 401 || error.status === 403) { 
      console.log('unAuthorized access'); 
      this._router.navigate(['/login'], { queryParams: { returnUrl: this._router.url } }); 
     } 

     if (error['name'] === 'TimeoutError') { 
      console.log(this._notify) // undefined here 
     } 
     return Observable.throw(error); 
     }); 
    } 

    get(url: string, options?: RequestOptionsArgs): Observable<Response> { 
    url = this.updateUrl(url); 
    return super.get(url, this.getRequestOptionArgs(options)); 
    } 

    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { 
    url = this.updateUrl(url); 
    return super.post(url, body, this.getRequestOptionArgs(options)); 
    } 

    put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { 
    url = this.updateUrl(url); 
    return super.put(url, body, this.getRequestOptionArgs(options)); 
    } 

    delete(url: string, options?: RequestOptionsArgs): Observable<Response> { 
    url = this.updateUrl(url); 
    return super.delete(url, this.getRequestOptionArgs(options)); 
    } 

    private updateUrl(req: string) { 
    return environment.origin + req; 
    } 

    private getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs { 

    const request_headers = { 
     'Content-Type': 'application/json', 
     'X-Requested-By': 'api-client' 
    }; 

    if (options == null) { 
     options = new RequestOptions(); 
    } 
    if (options.headers == null) { 
     options.headers = new Headers(request_headers); 
    } 

    return options; 
    } 
} 

我的HTTP攔截廠

// dependencies 
import { XHRBackend, Http, RequestOptions } from '@angular/http'; 
import { HttpInterceptor } from './http-interceptor'; 
import { Router } from '@angular/router'; 

// shared services 
import { NotificationService } from '@sharedServices/notification/notification.service'; 

export function HttpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, _router: Router, _notify: NotificationService): Http { 
    return new HttpInterceptor(xhrBackend, requestOptions, _router, _notify); 
} 

的通知裏面的catch塊被不確定的。我無法弄清楚爲什麼會發生這種情況。

我對此很新穎。我究竟做錯了什麼?

+1

聽起來像是你應該看看新的'HttpClient',因爲有攔截建設項。 https://angular.io/guide/http – cyrix

+0

謝謝。 :)。我會看看它。你能說我在這裏做錯了什麼@cyrix –

回答

1

你有沒有做到: -

'deps' :[NotificationService]

在這樣的模塊: -

{ 
'provide' : Http, 
'useFactory' : HttpFactory, 
'deps' : [NotificationService] 

} 
+0

我忘了那個。完全。謝謝 –