调用REST服务的最佳做法和使用angular2捕获的全局错误 [英] Best practice for call REST services and global error catching with angular2
问题描述
有没有经验? p>
目前我发现的最佳做法是首先创建全局服务,并创建与 http相关的方法
那里。即获取,放置,发布,删除请求等,而不是通过使用这些方法调用您的API服务请求,并且
使用catch块和显示消息捕获错误,例如: -
Global_Service.ts
import {Injectable} from'@ angular / core';来自'@ angular / http'的
/ pre>
import {Http,Response,RequestOptions,Headers,Request,RequestMethod};
import {Observable} from'rxjs / Rx';
import'rxjs / Rx';
@Injecable()
export class GlobalService {
public headers:Headers;
公共请求:RequestOptions;
public res:Response;
构造函数(public http:Http){}
public PostRequest(url:string,data:any):any {
this.headers = new Headers();
this.headers.append(Content-type,application / json);
this.headers.append(授权,承载+键);
this.requestoptions = new RequestOptions({
method:RequestMethod.Post,
url:url,
headers:this.headers,
body: JSON.stringify(data)
})
return this.http.request(new Request(this.requestoptions))
.map((res:Response)=> {
return [{status:res.status,json:res}]
})
.catch((error:any)=> {// catch错误在这里使用catch块
if(error.status === 500){
//显示您的消息错误
}
else if(error.status === 400){
//在此显示您的消息错误
}
});
}
public GetRequest(url:string,data:any):any {...}
public PutRequest(url:string,data:any) :任何{...}
public DeleteRequest(url:string,data:any):any {...}
}
更好地提供这种服务作为引导您的应用程序时的依赖关系: -
bootstrap(APP,[GlobalService,.....])
比你想要调用请求使用这些globalservice方法调用请求,如下所示: -
demo.ts
export class Demo {
...
constructor(public GlobalService:GlobalService){}
getMethodFunction(){
this.GlobalService.PostRequest(url,data)
.subscribe(res => {console.log(res),
err => {console.log(err)}
});
}
另见
-
希望这可以帮助你。
What is the best practice to call with angular2 REST SERVICE and catch any global exceptions to handle errors and show custom messages.
Has anyone experiences with that?
The best practice i have found so far is to firstly create global service and create your method related to http
there. i.e for Get, Put,Post ,Delete request etc. than via using these methods call your API service request and
catch there errors using catch block and display message as you want for example :-
Global_Service.ts
import {Injectable} from '@angular/core';
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/Rx';
@Injecable()
export class GlobalService {
public headers: Headers;
public requestoptions: RequestOptions;
public res: Response;
constructor(public http: Http) { }
public PostRequest(url: string, data: any): any {
this.headers = new Headers();
this.headers.append("Content-type", "application/json");
this.headers.append("Authorization", 'Bearer ' + key );
this.requestoptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: this.headers,
body: JSON.stringify(data)
})
return this.http.request(new Request(this.requestoptions))
.map((res: Response) => {
return [{ status: res.status, json: res }]
})
.catch((error: any) => { //catch Errors here using catch block
if (error.status === 500) {
// Display your message error here
}
else if (error.status === 400) {
// Display your message error here
}
});
}
public GetRequest(url: string, data: any): any { ... }
public PutRequest(url: string, data: any): any { ... }
public DeleteRequest(url: string, data: any): any { ... }
}
better to provide this service as dependency at the time of bootstraping your app like this :-
bootstrap (APP, [GlobalService, .....])
than whereever you want to call the request call the request using these globalservice methods like this :-
demo.ts
export class Demo {
...
constructor(public GlobalService: GlobalService) { }
getMethodFunction(){
this.GlobalService.PostRequest(url, data)
.subscribe(res => {console.log(res),
err => {console.log(err)}
});
}
see also
catch is not working for http.get Angular2.
Hope this may help you.
这篇关于调用REST服务的最佳做法和使用angular2捕获的全局错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!