【发布时间】:2019-01-18 00:28:52
【问题描述】:
我创建了一个实现 httpinterceptor 的角度拦截器。它适用于 http GET 请求。但 POST 请求失败。
我的拦截器代码如下。
import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders,HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
constructor() { console.log('enter constructor');
}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
console.log('entered interceptor')
const token = localStorage.getItem('sessiontoken') != null ? localStorage.getItem('sessiontoken') : 'notoken';
const authReq = req.clone({ headers: req.headers.set("Authorization", token) });
return next.handle(authReq);
}
}
模块添加了提供者。
providers: [
{
provide: HTTP_INTERCEPTORS,
useValue: { disableClose: true, minWidth: 400, hasBackdrop: true },
useClass: MyHttpInterceptor,
multi: true
}
]
在组件中我已经导入了 httpClient 并使用了 this.http.post
import { HttpClient } from '@angular/common/http';
【问题讨论】:
-
为什么你在同一个提供者声明中有useValue和useClass?
-
我删除了,还是不行
标签: angular angular6 angular-http-interceptors