我想对整个Angular 4应用程序使用相同的请求选项。特别是我想传递withCredentials: environment.xhrWithCredentials请求(基于当前环境)。
但是我认为为这样的每个请求设置选项不是一个好主意:
this.http.get('/api', {withCredentials: environment.xhrWithCredentials})
Run Code Online (Sandbox Code Playgroud)
有没有办法HttpClient为整个应用程序一次设置默认选项?
我尚未使用它,但是基于https://angular.io/guide/http,您需要拦截所有请求并在此处设置基本属性。
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor() {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({withCredentials: true /*environment.xhrWithCredentials*/});
return next.handle(authReq);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2098 次 |
| 最近记录: |