gpt4 book ai didi

Angular 4 : Using HTTP interceptor + RxJS timeout operator error

转载 作者:太空狗 更新时间:2023-10-29 18:18:58 25 4
gpt4 key购买 nike

我正在尝试为使用 HttpClient 执行的每个 HTTP 请求设置 10 秒的默认超时。我有一个拦截器可以向 header 添加一些值,我读过要设置超时,您必须使用“超时”RxJS 运算符,如下所示:

import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpHeaders } from '@angular/common/http';

import { Observable } from 'rxjs/Observable';
import { timeout } from 'rxjs/operators/timeout';

@Injectable()
export class APIInterceptor implements HttpInterceptor {
constructor() {

}

intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

// NOTE: The following 'authToken' and 'user_id' values are obtained through a global service
const authReq = req.clone({
headers: new HttpHeaders ({
'Content-Type': 'application/json',
'X_TOKEN_AUTH': authToken,
'X_IDUSER': user_id
})
});

const API_TIMEOUT = 10000;

//console.log('HEADER: ', authReq)

return next.handle(authReq).timeout(API_TIMEOUT); // Set a timeout for the requests
}
}

在添加超时功能之前,一切正常, header 中注入(inject)了身份验证 token 和用户 ID。但是,现在我收到以下错误:

next.handle(...).timeout is not a function

我做错了什么吗?谢谢!

最佳答案

如果你想使用运算符的“补丁”样式,你需要从 rxjs/add/operator/* 导入它:

import 'rxjs/add/operator/timeout';

'rxjs/operators/timeout'方法在使用pipe()时使用。

关于 Angular 4 : Using HTTP interceptor + RxJS timeout operator error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com