角度cli rxjs运算符导入 [英] angular cli rxjs operator imports

查看:68
本文介绍了角度cli rxjs运算符导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用angular cli创建的组件中,我从未添加import 'rxjs/add/operator/first';

但是,使用this.route.params.first()仍然有效.为什么?

是否有可能不必在每个文件中导入很多运算符?

解决方案

一样导入

import 'rxjs/add/operator/first';

请参阅修补程序Observable原型中的运算符.参见.

这种导入不需要基于每个文件进行.它们只需要导入一次-之后,可以通过原型调用该运算符.

此外,如果您要导入的另一个库恰好使用了该导入,则将对原型进行修补,并且操作员将可用.因此,某些库(例如Angular)会显式导入运算符,而不会修补原型. (这样,客户端代码将不依赖于任何其他由Angular部署的原型补丁导入,并且可以在不破坏客户端代码的情况下删除Angular中的导入.)

文档中提供了有关各种导入机制的更多信息.. >

in my component created with angular cli I never added import 'rxjs/add/operator/first';

however, using this.route.params.first() still worked. why?

Is it possible to not have to import lots of operators in every file?

解决方案

Imports like

import 'rxjs/add/operator/first';

see the operator patched into the Observable prototype. See the source.

Such imports do not need to be made on a per-file basis. They only need to be imported once - after which, the operator is callable via the prototype.

Also, if another library you are importing happens to use that import, the prototype will have been patched and the operator will be available. For this reason, some libraries - like Angular - import the operators explicitly without patching the prototype. (That way, client code won't be dependent upon any protoype-patching imports that would otherwise have been put in place by Angular and imports within Angular can be removed without breaking client code.)

There is more information about the various importing mechanism in the docs.

这篇关于角度cli rxjs运算符导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆