Angular 7:如何增加angular请求的时间
Angular 7: How to increase the time of an angular request
我需要增加 angular 应用程序的请求时间,因为使用慢速互联网连接会超时。
我尝试了下面的代码但出现错误。
this.http.post(url, body, { headers: headers })
.timeout(100, this.handleTimeout)
.map(response =>{
return response;
})
.catch(this.handleErrors);
Property 'timeout' does not exist on type
'Observable'.ts(2339)
使用拦截器也不成功
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).timeout(5000);
}
Property 'timeout' does not exist on type
'Observable>'.ts(2339)
谢谢
使用 Rxjs 6,你将不得不使用 .pipe
然后使用像 .timeout
这样的运算符
所以你的实现应该是这样的:
import {
timeout,
map,
catch
} from 'rxjs/operators';
this.http.post(url, body, { headers: headers })
.pipe(
timeout(100, this.handleTimeout),
map(response =>{
return response;
}),
catch(this.handleErrors);
适合我的最终解决方案:
import { timeout} from 'rxjs/operators';
return this.http.get(`${url}`).pipe(
timeout(1000)
);
感谢大家的帮助。
我需要增加 angular 应用程序的请求时间,因为使用慢速互联网连接会超时。
我尝试了下面的代码但出现错误。
this.http.post(url, body, { headers: headers })
.timeout(100, this.handleTimeout)
.map(response =>{
return response;
})
.catch(this.handleErrors);
Property 'timeout' does not exist on type 'Observable'.ts(2339)
使用拦截器也不成功
@Injectable()
export class AngularInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).timeout(5000);
}
Property 'timeout' does not exist on type 'Observable>'.ts(2339)
谢谢
使用 Rxjs 6,你将不得不使用 .pipe
然后使用像 .timeout
这样的运算符
所以你的实现应该是这样的:
import {
timeout,
map,
catch
} from 'rxjs/operators';
this.http.post(url, body, { headers: headers })
.pipe(
timeout(100, this.handleTimeout),
map(response =>{
return response;
}),
catch(this.handleErrors);
适合我的最终解决方案:
import { timeout} from 'rxjs/operators';
return this.http.get(`${url}`).pipe(
timeout(1000)
);
感谢大家的帮助。