Angular and RxJS imports

此生再无相见时 提交于 2019-11-30 12:14:15

Why not have a file(ex: rxjs-extensions.ts) with your required rxjs observable class extensions and operators?

// Observable class extensions
import 'rxjs/add/observable/throw';

// Observable operators
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';

And then in your main module (ex app.module.ts) import from this file:

import './rxjs-extensions';

And in your main component (ex: app.component.ts) just import Observavle:

import { Observable } from 'rxjs/Rx';

This is how it is covered on the main angular tutorial.

Starting from WebStorm 2016.3 (I believe), you have an option to blacklist certain imports. Editor > Code Style > StypeScript

Do not import exactly from: [rxjs]

Additionally, there is a flag available in tslint to prohibit global imports:

{
  "rules": {
    "import-blacklist": [true, "rxjs"]
  }
}
Sandip Jaiswar

You can use all operators by using this:

import * as Rx from "rxjs/Rx";

Rx.Observable.of(1,2,3,4,5);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!