Rxjs运算符的Typescript import语句 [英] Typescript import statements for Rxjs operators

查看:111
本文介绍了Rxjs运算符的Typescript import语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Angular项目,其中不导入RxJs库运算符,
map,do等。源中似乎没有通配符导入。


我的Ionic和Angular项目都执行:

I have one Angular project where things like the RxJs library operators, map,do etc are not imported. There appears to be no wildcard imports in the source.

Both my Ionic and Angular projects do:

import { Observable } from 'rxjs';

然后我有一个离子项目抱怨如果我没有说明导入。

Then I have an Ionic project which complains if I don't explicity state the imports.

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';

此外,在Ionic项目中,我似乎无法找到 refCount 。虽然现在,我开始认为我需要一个像rx.lite.js这样的特定包...我不知道如何将rx.lite.js和rx.lite-aggregates.js添加到Angular项目中在node_modules文件夹中。我在Ionic项目中没有这样做。对于Angular项目,package.json中没有明确的引用。某种传递依赖可能呢?

Furthermore in the Ionic project, I can't seem to locate refCount in node_modules folders for RxJS either. Though now, I'm beginning to think I need a specific package like rx.lite.js for that... I have no idea how rx.lite.js and and rx.lite-aggregates.js got added to the Angular project in the node_modules folder. I don't get that in my Ionic project. There's not an explicit reference to them in the package.json for the Angular project. Some sort of transitive dependency perhaps?

Angular项目是我从Git仓库得到的东西,所以我想知道是否有某种配置文件可以做到这一点在项目的其他地方。
我想知道是否有人可以帮我解释一下这个谜。

The Angular project was something I got from a Git repo, so I'm wondering if there is some sort of configuration file that does this somewhere else in the project. I'm wondering if someone can help shed some light on this mystery for me.


  • Angular项目包。 json rxjs依赖

  • Angular project package.json rxjs dependency

"rxjs": "^5.1.0",


  • Angular项目tsconfig.json

  • Angular project tsconfig.json

    {
      "compileOnSave": false,
      "compilerOptions": {
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [
          "es2016"
        ],
        "module": "commonjs",
        "moduleResolution": "node",
        "outDir": "../dist/out-tsc-e2e",
        "sourceMap": true,
        "target": "es6",
        "typeRoots": [
          "../node_modules/@types"
        ]
      }
    }
    


  • 离子项目package.json rxjs依赖

  • Ionic project package.json rxjs dependency

    "rxjs": "5.1.1",
    


  • 离子项目tsconfig.json

  • Ionic project tsconfig.json

    {
       "compilerOptions": {
         "allowSyntheticDefaultImports": true,
         "declaration": false,
         "emitDecoratorMetadata": true,
         "experimentalDecorators": true,
         "lib": [
           "dom",
           "es2015"
         ],
         "module": "es2015",
         "moduleResolution": "node",
         "sourceMap": true,
         "target": "es5"
       },
       "include": [
         "src/**/*.ts"
       ],
      "exclude": [
        "node_modules"
       ],
     "compileOnSave": false,
     "atom": {
       "rewriteTsconfig": false
     }
    

    }

    我一直试图理解tsconfig.json。请参阅此链接

    I have been trying to make sense of tsconfig.json. See this link

    推荐答案

    这很可能取决于源代码的结构。

    This most likely depends on the structure of your source code.

    一般来说,你永远不应该从'rxjs'并始终导入特定模块。例如:

    In general, you should never import from 'rxjs' and always import a specific module. For example:

    import { Observable } from 'rxjs/Observable';
    

    当你从'rxjs'导入{Observable}时$ c>您正在导入 Rx.ts 文件,该文件已添加所有运算符,这意味着您无需调用任何 import'rxjs / add / operator /...;;因为已经导入了运算符。

    When you do import { Observable } from 'rxjs' you're importing Rx.ts file that adds all the operators already which means you don't need to call any import 'rxjs/add/operator/...'; because the operators are already imported.

    例如,如果你有一个Angular项目并且在根目录中你从'rxjs'添加 import {Observable}的模块即使你没有使用任何 Observable <你也永远不需要单独导入任何运算符根模块中的/ code>。

    For example if you have an Angular project and in the root module you add import { Observable } from 'rxjs' you'll never need to import any operators separately even though you're not using any Observable in the root module at all.

    我认为你的情况完全一样。它只取决于您从'rxjs'导入的位置,如果它在您尝试使用任何运算符之前发生。

    I think your situation is exactly the same. It just depends on where you're importing from 'rxjs' and if it happens before you try to use any of the operators.

    因此 angular-cli 默认情况下黑名单从'rxjs'导入。

    For this reason angular-cli by default blacklists importing from 'rxjs'.

    这篇关于Rxjs运算符的Typescript import语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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