What is the tslint blacklist and why does angular-cli default rxjs on the list in tslint.json?

别来无恙 提交于 2019-11-29 13:22:06

This is because you should (at least in browser apps) never include from 'rxjs' and always use more specific for example 'rxjs/Observable' or 'rxjs/BehaviorSubject'.

When you include 'rxjs' you're in fact including this file: https://github.com/ReactiveX/rxjs/blob/master/index.js which includes the entire bundled RxJS library (all operators, schedulers, etc.). So you're including a lot of things you don't even use and your app grows bigger than necessary (I think tree-shaking with webpack2 doesn't help and once the code is included it'll be part of the final package, but I might be wrong).

I think it's ok to import directly from 'rxjs' in node apps (eg. backend apps) where it doesn't matter that much that it contains also code you're not going to use and this way is just easier to use.

The reason for it is because of the change in the tslint.json as they dont want the whole module of rxjs to be loaded on Angular Application as it makes it heavier in dependency loading. Instead load only sub modules which are necessary for your application.

To solve it, change

import { Observable, BehaviorSubject } from 'rxjs';

to:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

This link explains a little more clearly:

https://fullstack-developer.academy/resolving-import-is-blacklisted-tslint-error-for-rxjs-and-angular/

Essentially, when you import like

import { Observable, BehaviorSubject } from 'rxjs';

or

import { Observable, BehaviorSubject } from 'rxjs/Rx';

it pulls in Rx.js which will import EVERYTHING (Observable, BehaviorSubject, ReplaySubject,Subscriber, Subscription, etc...) in the rxjs library which is a lot more dependencies than you are actually after. Unless you really need to use most of these in the file you are importing in, you are better off importing each dependency on its own line like

import { Observable } from 'rxjs/Observable';
import { Subscription} from 'rxjs/Subscription';

This results in fewer dependencies being pulled in and hopefully a smaller compiled filesize.

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