我是Angular的新手,我像这样使用httpClient:
public getFocusAreaDataWithHttpClient() {
this.http.get(Api.focusAreaApi).subscribe({
next(response) {
console.log("httpClient handle data");
console.dir(response);
},
error(err) {
console.error("Error: " + err);
},
complete() {
console.log("Completed");
}
});
}我想封装一个服务(apiManager.service)来提供一个公共方法,这样其他服务就可以使用它来获取数据(需要传递apiurl),我希望是这样的:
export class ApiManager {
constructor(private http: HttpClient) {}
// define request header , content-Type , auth token ,and some common config here
// and there should be 2 method - get and post
}其他服务可以像这样使用它:
apiManager.get(apiurl,parameters).subscript...
apiManager.post(apiurl,parameters).subscript...但是我不知道如何封装它,如果有人能给我一些演示来展示如何封装一个管理器类来提供数据获取功能,我将非常感激。
发布于 2019-05-29 21:52:34
在Angular中,这种类型的活动是通过services完成的。
如果您使用Angular CLI (您一定要使用它),只需在终端中输入ng generate service api-manager,它将为您创建所有的样板文件。
https://stackoverflow.com/questions/56361913
复制相似问题