0
私は401エラーとタイムアウトエラーを監視するHTTPインターセプタを持っています。私がその中のサービスをインポートするとき。サービスが私の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ブロックが未定義ます内側に通知
定義されていないと言っています。なぜこのようなことが起こるのか理解できませんでした。
私はこれにかなり新しいです。私は間違って何をしていますか?
新しい 'HttpClient'になりますようにそれはインターセプタが構築インを持っているので、サウンド。 https://angular.io/guide/http – cyrix
ありがとうございます。 :)。私はそれを見ます。 @cyrix –