【发布时间】:2017-11-29 11:02:06
【问题描述】:
我正在寻找 Angular 的通用 http 请求,例如在 vanilla JS 中,您可以将请求方法作为输入参数('POST'、'GET'...)。
我尝试使用开关盒,但效果不佳。
Request(UserDetails, endpoint, method): Observable<Comment[]> {
const bodyString = UserDetails;
const headers = new Headers({ 'Content-Type': 'application/json'});
const options = new RequestOptions({headers: headers});
const meth = method
console.log(meth)
let request;
switch(meth) {
case 'post': {
request = this.http.post;
console.log(request)
break;
}
case 'get': {
request = this.http.post;
break;
}
case 'patch': {
request = this.http.patch;
break;
}
case 'delete': {
request = this.http.delete;
break;
}
};
return request(endpoint, bodyString, options)
.map((res: Response) => res.json())
.catch((error: any) => Observable.throw(error.json() || 'server
error'));
}
【问题讨论】:
-
需要更多字符
标签: angular typescript xmlhttprequest switch-statement