问题描述
我正在将 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';
现在使用新语法我看到
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?
推荐答案
来自 rxjs 5.5,catch
已重命名为 catchError
函数以避免名称冲突.
From rxjs 5.5, catch
has been renamed to catchError
function to avoid name clash.
由于操作符独立于 Observable 可用,操作符名称不能与 JavaScript 关键字限制冲突.因此,某些运算符的可管道版本的名称已更改.
import { catchError } from 'rxjs/operators';
对于throw
,你可以使用ErrorObservable
.
import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
ErrorObservable.create(new Error("oops"));
rxjs 6
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 导入语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!