Angular 6 RXJS导入语法? [英] Angular 6 RXJS Import Syntax?
问题描述
我正在将Angular 5应用程序迁移到最新的CLI和Angular 6 RC,并且我的所有Observable导入均已损坏.我看到Angular 6改变了导入的方式,但是我找不到关于语法如何工作的明确参考.
I'm migrating an Angular 5 app to the latest CLI and Angular 6 RC and all of my Observable imports are broken. I see that Angular 6 changes the way the imports work, but I can't find any definite reference as to how the syntax works.
我有5个,而且效果很好:
I had this in 5 and it worked fine:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
现在有了新语法,我看到了
Now with the new syntax I see that
import { Observable, Subject, throwError} from 'rxjs';
import { map } from 'rxjs/operators';
前两行可以编译,但是例如,我不知道如何获取和抛出.在代码中使用.map()还会引发构建错误.
The first two lines compile, but I can't figure out how to get catch and throw for example. .map() also throws a build error when used in code.
有人提到它应该如何工作吗?
Anybody have a reference to how this is supposed to work?
推荐答案
From rxjs 5.5, catch
has been renamed to catchError
function to avoid name clash.
由于可以独立于Observable使用运算符,因此运算符名称不能与JavaScript关键字限制冲突.因此,某些运算符的可移植版本的名称已更改.
Due to having operators available independent of an Observable, operator names cannot conflict with JavaScript keyword restrictions. Therefore the names of the pipeable version of some operators have changed.
import { catchError } from 'rxjs/operators';
对于throw
,您可以使用ErrorObservable
.
import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
ErrorObservable.create(new Error("oops"));
rxjs 6
使用 throwError .
import { throwError } from 'rxjs'
throwError(new Error("oops"));
现在,您还必须通过管道传递运算符,而不是将它们直接链接到可观察的
Also you will now have to pipe the operators instead of directly chaining them to the observable
这篇关于Angular 6 RXJS导入语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!