gpt4 book ai didi

httprequest - Http post并以角度6获取请求

转载 作者:行者123 更新时间:2023-12-03 09:57:43 25 4
gpt4 key购买 nike

在用于 http get 和 post 的 angular 5.2.x 中,我有以下代码:

post(url: string, model: any): Observable<boolean> {

return this.http.post(url, model)
.map(response => response)
.do(data => console.log(url + ': ' + JSON.stringify(data)))
.catch(err => this.handleError(err));
}
get(url: string): Observable<any> {

return this.http.get(url)
.map(response => response)
.do(data =>
console.log(url + ': ' + JSON.stringify(data))
)
.catch((error: any) => Observable.throw(this.handleError(error)));
}

在角度 6 中它不起作用。

我们如何进行 HTTP 发布或获取请求?

最佳答案

更新 :
在角度 7 中,它们与 6 相同

在角 6

live example 中找到的完整答案

  /** POST: add a new hero to the database */
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
catchError(this.handleError('getHeroes', []))
);
}

这是因为 pipeable/lettable operators现在 angular 可以使用 tree-shakable并删除未使用的导入并优化应用程序

一些 rxjs 功能已更改
do -> tap
catch -> catchError
switch -> switchAll
finally -> finalize

更多在 MIGRATION

和导入路径

对于 JavaScript 开发者,一般规则如下:

rxjs:创建方法、类型、调度程序和实用程序
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent } from 'rxjs';

rxjs/operators:所有可管道操作符:
import { map, filter, scan } from 'rxjs/operators';

rxjs/webSocket:Web 套接字主题实现
import { webSocket } from 'rxjs/webSocket';

rxjs/ajax:Rx ajax 实现
import { ajax } from 'rxjs/ajax';

rxjs/testing:测试工具
import { TestScheduler } from 'rxjs/testing';

为了向后兼容,您可以使用 rxjs-compat

关于httprequest - Http post并以角度6获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50196282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com