作者:陈宥治政和瑋男 | 来源:互联网 | 2022-12-18 13:12
在角度5.2.x的http get和post我有这个代码:
post(url: string, model: any): Observable {
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 {
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帖子或获取请求?
1> unos baghaii..:
更新:在角度7中,它们与6相同
在角6
实例中的完整答案
/** POST: add a new hero to the database */
addHero (hero: Hero): Observable {
return this.http.post(this.heroesUrl, hero, httpOptions)
.pipe(
catchError(this.handleError('addHero', hero))
);
}
/** GET heroes from the server */
getHeroes (): Observable {
return this.http.get(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 /运算符:所有可管道运算符:
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