为打字稿中的 Observable 导入 .of()
import .of() for Observable in typescript
我正在为我的 angular 2 测试项目 (TypeScript) - https://github.com/qdouble/angular-webpack2-starter 使用这个很棒的存储库。我需要使用 Observable.of(..)。当我尝试导入它时:
import { Observable } from "rxjs/Observable";
import { of } from 'rxjs/observable/of';
我得到:
Property 'of' does not exist on type 'typeof Observable'.
我也试过以下方法:
import { Observable } from "rxjs/Observable";
import { of } from 'rxjs/add/observable/of'; // notice 'add'
我得到了:
node_modules/rxjs/add/observable/of"' has no exported member 'of'.
那么,如何为 Observable 导入这个 Of() 静态方法呢???
您不必从 'rxjs/add/observable/of'
导入 {of}
。可以直接使用
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";
或者您可以从 "rxjs/Rx" 导入 Observable
,它捆绑了所有运算符。 不良做法
import { Observable } from "rxjs/Rx";
2018-01-26 更新:RxJS v5.5+ 管道运算符
来自https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md
Starting in version 5.5 we have shipped "pipeable operators", which can be accessed in rxjs/operators (notice the pluralized "operators"). These are meant to be a better approach for pulling in just the operators you need than the "patch" operators found in rxjs/add/operator/*.
现在 "patching" 导入将被弃用,最好使用严格导入。
import { of as observableOf } from 'rxjs/observable/of'
并像那样使用它
const myObs$: Observable<number> = observableOf(1, 2, 3)
RxJS 6
从 RxJS 6 开始,由于更 直观的 API 表面 [=45=,导入大大简化并且现在造成的混淆更少]:
像of
这样的创建方法是直接从'rxjs'
:
导入的
import { of } from 'rxjs';
所有管道运算符都是从'rxjs/operators'
导入的,例如:
import { tap, map } from 'rxjs/operators';
迁移:
如果您直接从 RxJS 5 更新,您甚至可以使用 RxJS 的 TSLint 规则,它会自动将您的导入修复到更新的、简化的 API:
npm i -g rxjs-tslint
rxjs-5-to-6-migrate -p [path/to/tsconfig.json]
我正在为我的 angular 2 测试项目 (TypeScript) - https://github.com/qdouble/angular-webpack2-starter 使用这个很棒的存储库。我需要使用 Observable.of(..)。当我尝试导入它时:
import { Observable } from "rxjs/Observable";
import { of } from 'rxjs/observable/of';
我得到:
Property 'of' does not exist on type 'typeof Observable'.
我也试过以下方法:
import { Observable } from "rxjs/Observable";
import { of } from 'rxjs/add/observable/of'; // notice 'add'
我得到了:
node_modules/rxjs/add/observable/of"' has no exported member 'of'.
那么,如何为 Observable 导入这个 Of() 静态方法呢???
您不必从 'rxjs/add/observable/of'
导入 {of}
。可以直接使用
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/of";
或者您可以从 "rxjs/Rx" 导入 不良做法Observable
,它捆绑了所有运算符。
import { Observable } from "rxjs/Rx";
2018-01-26 更新:RxJS v5.5+ 管道运算符
来自https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md
Starting in version 5.5 we have shipped "pipeable operators", which can be accessed in rxjs/operators (notice the pluralized "operators"). These are meant to be a better approach for pulling in just the operators you need than the "patch" operators found in rxjs/add/operator/*.
现在 "patching" 导入将被弃用,最好使用严格导入。
import { of as observableOf } from 'rxjs/observable/of'
并像那样使用它
const myObs$: Observable<number> = observableOf(1, 2, 3)
RxJS 6
从 RxJS 6 开始,由于更 直观的 API 表面 [=45=,导入大大简化并且现在造成的混淆更少]:
像
导入的of
这样的创建方法是直接从'rxjs'
:import { of } from 'rxjs';
所有管道运算符都是从
'rxjs/operators'
导入的,例如:import { tap, map } from 'rxjs/operators';
迁移:
如果您直接从 RxJS 5 更新,您甚至可以使用 RxJS 的 TSLint 规则,它会自动将您的导入修复到更新的、简化的 API:
npm i -g rxjs-tslint
rxjs-5-to-6-migrate -p [path/to/tsconfig.json]