我正在尝试在HttpRequest和HttpResponse的范围内添加微调器。在app.module.ts的提供程序列表中添加名为HttpServiceInterceptor的服务后,抛出ngOriginalError。
http.interceptor.ts:
Collections.sort(yourArrayList, new YourComparator());
app.module.ts:
import {Injectable} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/finally';
import { CommonService } from '../../services/common.service';
@Injectable()
export class HttpServiceInterceptor implements HttpInterceptor{
constructor(private commonService : CommonService){}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let url : string =request.url;
let subUrl : string =url.substring(url.lastIndexOf("/"),url.length);
if(subUrl =='/searchData' || subUrl =='/searchReportsData' || subUrl =='/searchRerunData' || subUrl =='/reRun'){
this.commonService.startSpinner();
return next.handle(request).finally(()=>this.commonService.stopSpinner());
}else{
return next.handle(request);
}
}
}
CommonService(包含已实现的方法):
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: HttpServiceInterceptor, multi: true } ]
错误:
startSpinner() {
console.log("start spinner");
this.startspinner.next();
}
stopSpinner() {
console.log("stop spinner");
this.stopspinner.next();
}