angular cli rxjs 操作符导入 [英] angular cli rxjs operator imports

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

问题描述

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

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

是否可以不必在每个文件中导入大量运算符?

解决方案

Imports like

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.

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

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