问题
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