Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 开源Rest客户端库_Angular_Angular Httpclient - Fatal编程技术网

Angular 开源Rest客户端库

Angular 开源Rest客户端库,angular,angular-httpclient,Angular,Angular Httpclient,您好,我想知道是否有一个类似于android、apache http client for java或RestTemplate for SpringBoot中的改装的angular js库 我能够成功地使用rxjs,但我有很多api调用,我正在寻找是否有更好的解决方案,我不知道 我的一些发现是: [家长参考] 看起来它们要么已经过时,要么最近没有维护 有人能帮我找一个快速的包装吗 编辑: 添加了我已经在项目中使用的示例代码 import {HttpClient} from '@angul

您好,我想知道是否有一个类似于android、apache http client for java或RestTemplate for SpringBoot中的改装的angular js库

我能够成功地使用rxjs,但我有很多api调用,我正在寻找是否有更好的解决方案,我不知道

我的一些发现是:

  • [家长参考]
看起来它们要么已经过时,要么最近没有维护

有人能帮我找一个快速的包装吗

编辑:

添加了我已经在项目中使用的示例代码

import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Tweet} from '../api/models/response/Tweet';
import {Injectable} from '@angular/core';
import {environment} from '../../environments/environment';
import {AuthenticationService} from '../api/services/authentication.service';

@Injectable({
  providedIn: 'root'
})
export class TweetsService {
  tweetsUrl = '/tweets/timeline?page=0&size=25';
  sendTweetUrl = '/tweets/create';

  constructor(private httpClient: HttpClient,
              private authenticationService: AuthenticationService) {

  }

  getTweets(): Observable<Tweet[]> {
    return this.httpClient.get<Tweet[]>(environment.resourceApiUrl + this.tweetsUrl);
  }

  sendTweet(tweet: Tweet): Observable<Tweet> {
    return this.httpClient.post<Tweet>(environment.resourceApiUrl + this.sendTweetUrl, tweet);
  }
}
从'@angular/common/http'导入{HttpClient};
从“rxjs”导入{Observable};
从“../api/models/response/Tweet”导入{Tweet};
从“@angular/core”导入{Injectable};
从“../../environments/environment”导入{environment};
从“../api/services/authentication.service”导入{AuthenticationService};
@注射的({
providedIn:'根'
})
导出类TweetsService{
tweetsUrl='/tweets/timeline?页面=0,大小=25';
sendTweetUrl='/tweets/create';
构造函数(私有httpClient:httpClient,
私有身份验证服务:身份验证服务){
}
getTweets():可观察{
返回this.httpClient.get(environment.resourceApiUrl+this.tweetsUrl);
}
sendTweet(tweet:tweet):可观察{
返回this.httpClient.post(environment.resourceApiUrl+this.sendTweetUrl,tweet);
}
}

根据我的评论,我认为这段代码相当简洁。如果你觉得它太整洁,你可以按照

export class HttpService {
  constructor(...) {...}

  get<T>(url) {
    return this.http.get<T>(environment.apiURL + url);
  }
}
导出类HttpService{
构造函数(…){…}
获取(url){
返回此.http.get(environment.apirl+url);
}
}

这将减少您的“样板代码”。但我绝对推荐您这样做,因为它清楚地分离了每个服务的逻辑(重新划分),这是一个非常好的实践

RxJS和HttpClient已经是一个包装器,可以向您抽象HTTP逻辑。你为什么不想用它呢?它的功能非常强大,重量相当轻,并且完全集成在角度上。有没有关于不使用它的反对意见?是的,我同意,但我需要编写很多锅炉板代码,我不是一个普通的角度编码器,因此,我正在寻找类似的东西,需要配置,所有端点都保留在一个位置。绝对没有要编写的样板代码来开始使用它。你所要做的就是把它导入你的应用程序模块,然后你就可以使用它了。但是,由于您似乎对它感到反感,我建议您做一些类似这样的事情,它不太灵活,功能不太强大,但更轻,可能更适合您的需要(尽管我真的建议您继续使用HttpClient)。我已经添加了代码,用于每个服务并注入内部组件。请建议我是否应该继续使用相同的!我补充了一个简单解释的答案。此外,如果您曾经使用Axios,请知道您将拥有相同数量的代码(导入、请求),因此您在使用Http时不会有更多的代码!我正在寻找类似的东西,这是一个包装!