如何使用HttpClient为angular 5中的web请求设置超时

如何使用HttpClient为angular 5中的web请求设置超时,angular,Angular,通过定义超时,您可以设置等待web请求的时间限制。 超时,是web请求无法通过的时间限制。例如,如果我定义了3秒的超时,那么请求数据时的web请求将在超过3秒时取消。 我希望我的web服务不超过3秒。我怎么做?我是angular.js领域的新手 import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {

通过定义超时,您可以设置等待web请求的时间限制。
超时,是web请求无法通过的时间限制。例如,如果我定义了3秒的超时,那么请求数据时的web请求将在超过3秒时取消。 我希望我的web服务不超过3秒。我怎么做?我是angular.js领域的新手

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs/Observable';
import { GLOBAL } from '../app.constantes';

@Injectable()
export class AppService{
public url: string;

constructor(
    public _http: HttpClient

){}
getAll(url,method,param): Observable<any>{
    let config={};
    config["timeout"]=3000;
    config["data"]=param ? param: {}; //in case of POST  this is the "data" property, with GET is "params" I believe..
    return this._http.post(url,config);

}
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders,HttpParams};
从“rxjs/operators”导入{map};
从“rxjs/Observable”导入{Observable};
从“../app.constantes”导入{GLOBAL};
@可注射()
导出类应用程序服务{
公共url:string;
建造师(
公共http:HttpClient
){}
getAll(url、方法、参数):可观察{
设config={};
配置[“超时”]=3000;
config[“data”]=param?param:{};//对于POST,这是“data”属性,我相信GET是“params”。。
返回此文件。\u http.post(url,config);
}

不,我不知道我是否不清楚我的问题。但是我引用了web请求的属性“timeout”。没有设置timeout。

您可以像这样使用timeout操作符:

this.http.post('myUrl', 
        MyData, {headers: Myheaders})
         .timeout(3000, new Error('timeout exceeded'))
         .map(res => res.json())
         .subscribe(
           data => this.ret = data,
           error => console.debug('ERROR', error),
           () => console.log('END')
         );

不,我不知道我是否对我的问题不清楚。但我指的是属性“timeout”web请求的超时。是否要在收到异步调用的响应后设置超时?超时,是web请求无法通过的时间限制。例如,如果我定义了3秒的超时,则请求数据时的web请求将在超过3秒时取消。此链接将帮助您进一步了解可管道运算符谢谢。我会在可能的时候尝试。我是否应该将任何其他库导入到我的示例中的库中?所以您希望您的rest调用在3分钟内完成?