如何在RxJS 6中仅导入使用过的运算符,例如较旧的RxJS,而无需rxjs-compat? [英] How to import only used operators in RxJS 6 like older RxJS without requiring rxjs-compat?
问题描述
以前,我只能使用以下代码导入二手操作员:
Previously I was able to import only used operators with this code:
import 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/finally';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/throw';
这会生成一个小捆绑包(vendor.ts).
如何在不要求rxjs-compat的情况下使用RxJS进行此操作?
将上面的代码更改为import 'rxjs';
会生成更大的包.
This generates a small bundle (vendor.ts).
How to do this with RxJS without requiring rxjs-compat?
Changing the code above to import 'rxjs';
generates a bigger bundle.
更新:
我遵循了您发布的所有答案,但效果不佳. 这是我更新的vendor.ts:
I followed all the answers you've posted but nothing works well. This is my updated vendor.ts:
import 'rxjs/Observable';
import 'rxjs/Subscription';
import 'rxjs/Subject';
import 'rxjs/observable/throw';
import 'rxjs/operators/map';
import 'rxjs/operators/mergeMap';
import 'rxjs/operators/catchError';
import 'rxjs/operators/finalize';
我也尝试使用'rxjs/add/operator/*'.
I also tried using 'rxjs/add/operator/*'.
这就是我导入rxjs的方式:
This is how I'm importing rxjs:
import {Observable} from 'rxjs/Observable';
import {Subscription} from 'rxjs/Subscription';
import {Subject} from 'rxjs/Subject';
import {_throw} from 'rxjs/observable/throw';
import {map} from 'rxjs/operators/map';
import {mergeMap} from 'rxjs/operators/mergeMap';
import {catchError} from 'rxjs/operators/catchError';
import {finalize} from 'rxjs/operators/finalize';
我根据此文档更改了Webpack 3的配置( https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md#build-and-treeshaking ),但没有任何效果.
I changed my Webpack 3 configuration according to this document (https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md#build-and-treeshaking) and nothing works.
最后,看看Webpack Bundle Analyzer的结果:
Finally, take a look at the Webpack Bundle Analyzer result:
该捆绑包包括所有运营商. 我发现了这个相关问题: https://github.com/angular/angular-cli/issues/9069
The bundle includes all operators. I found this related issue: https://github.com/angular/angular-cli/issues/9069
推荐答案
rxjs-compat
应该与rxjs
一起安装,它为旧式导入提供支持.
rxjs-compat
is supposed to be installed together with rxjs
, it provides the support for old-style imports.
可以使用与RxJS 5相同的方式来使用RxJS 6:
It's possible to use RxJS 6 the same way as RxJS 5:
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
此兼容性层有望在RxJS 7中删除.
This compatibility layer is expected to be removed in RxJS 7.
这篇关于如何在RxJS 6中仅导入使用过的运算符,例如较旧的RxJS,而无需rxjs-compat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!