angular cli rxjs operator imports

扶醉桌前 提交于 2020-01-05 05:56:59

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/45047174/angular-cli-rxjs-operator-imports

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