Angular 角度拦截器不会发出(POST)
使用角度10。我的error.interceptor.ts文件将不会调用。Angular 角度拦截器不会发出(POST),angular,Angular,使用角度10。我的error.interceptor.ts文件将不会调用。 注意'this.http.post(url,body,httpOptions);'这不叫吗? 我不知道为什么 import { Injectable } from '@angular/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpClient, HttpHeaders } from '@angular/common/ht
注意'this.http.post(url,body,httpOptions);'这不叫吗? 我不知道为什么
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AlertService } from '../../dashboard/alert/alert.service';
import { AuthService } from '../auth/auth.service';
import { Router } from '@angular/router';
import { GlobalService } from '../api/global.service';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(
private alertService: AlertService,
private http: HttpClient
) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError(err => {
let error = 'Ut oh, there was an error mate...';
if (err.error != null) {
if (err.statusText) {
error = err.statusText;
}
if (err.message) {
error += ': ' + err.message;
}
}
if (err.status === 401) {
// console.log("error 401");
// auto logout if 401 response returned from api
// this.authenticationService.logout();
// location.reload(true);
}
//
const url = 'log/logEvent';
const body = 'test';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
};
this.http.post<any>(url, body, httpOptions);
// Send error to the view for alert
this.alertService.error(error);
return throwError(error);
}));
}
}
从'@angular/core'导入{Injectable};
从“@angular/common/http”导入{HttpRequest、HttpHandler、HttpEvent、HttpInterceptor、HttpClient、HttpHeaders};
从“rxjs”导入{observatable,throwerr};
从“rxjs/operators”导入{catchError};
从“../../dashboard/alert/alert.service”导入{AlertService};
从“../auth/auth.service”导入{AuthService};
从'@angular/Router'导入{Router};
从“../api/global.service”导入{GlobalService};
@可注射()
导出类ErrorInterceptor实现HttpInterceptor{
建造师(
私人警报服务:警报服务,
私有http:HttpClient
) {}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
返回next.handle(request.pipe)(
catchError(err=>{
let error='哦,有一个错误伴侣…';
if(err.error!=null){
如果(错误状态文本){
error=err.statusText;
}
如果(错误消息){
错误+=':'+err.message;
}
}
如果(错误状态===401){
//控制台日志(“错误401”);
//如果api返回401响应,则自动注销
//this.authenticationService.logout();
//位置。重新加载(true);
}
//
const url='log/logEvent';
const body=‘test’;
常量httpOptions={
标题:新的HttpHeaders({
“内容类型”:“应用程序/json”,
})
};
this.http.post(url、body、httpOptions);
//将错误发送到视图以获取警报
this.alertService.error(错误);
返回投掷器(错误);
}));
}
}
您在App.module中注册了它吗?:)
应该是这样的:
providers: [{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }]
编辑:
我想这只是缺少的订阅。您的呼叫应该如下所示:
providers: [{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true }]
this.http.post(url,body,httpOptions).subscribe((res)=>null)
拦截器处理所有请求,从中操作或读取数据
如果您使用的是custome模块,则应将其导入cusotme.module.ts文件,否则应将其导入app.module.ts以使拦截器工作
providers: [
MyService,
MyOtherService,
...,
AuthGuard, // <- implements canActivate
RoleGuard, // <- implements canActivate
...,
// Interceptor
{
provide : HTTP_INTERCEPTORS,
useClass : TokenInterceptor, // Your intercepter
multi : true
}
],
// continue...
],
提供者:[
我的服务,
我的服务,
...,
AuthGuard,//是的,拦截器工作得很好。它根本不会这样做:this.http.post(url,body,httpOptions);嗯,好吧,其余的呢?它捕获到错误了吗?它捕获到错误了。所以我想将错误发送到服务器进行记录,但它不会发布?在我的上,忘记订阅!谢谢!知道这个问题哈哈,没问题!