typescript - 将 RxJS 与 TypeScript 结合使用

标签 typescript rxjs

我在尝试将 RxJS 与 TypeScript 和 Node.js 结合使用时遇到问题。我正在使用 NPM,并且包含了 rxjs-es 版本 5。我也在使用 Typings,并且包含了 es6-shimrx.all ,像这样:

{
  "ambientDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654",
    "rx.all": "registry:dt/rx.all#2.2.28+20160316155526"
  }
}

下面是我的 tsconfig.json 文件。

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/browser",
    "typings/browser.d.ts"
  ]
}

在 TypeScript 文件中,我正在尝试执行以下操作:

从'rxjs/Observable'导入{Observable};

当我尝试编译脚本时,出现以下错误:

typings/main/ambient/rx.all/index.d.ts(10,11): error TS2304: Cannot find name 'Rx'.

即使我尝试在我的脚本中使用或不使用 RxJS,也会发生这种情况,因此问题与 TypeScript 类型有关。

为了使用 RxJS,我需要使用 typings 和 NPM 安装什么?特别是,我对使用 Observables 很感兴趣。在谷歌上搜索了几个小时后,我似乎无法弄清楚我需要什么才能让它工作。我试过the following ,但没有任何运气。我还在 NPM 和 typings 中尝试了许多 rx 包的组合,但没有找到任何有效的方法。非常感谢任何帮助!

最佳答案

RxJS 5 是用 typescript 编写的,它的类型定义默认包含在已发布的模块中,您可能不需要为 rx.all 打字。实际上,这些类型定义是为以前版本的 RxJS 编写的。只需卸载 rx.all 类型定义并尝试在没有这些的情况下导入。

关于typescript - 将 RxJS 与 TypeScript 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36933788/

相关文章:

angular - Ionic 3 - 想要将对象数组转换为 Controller 中的字符串数组

reactjs - Cypress 在我的 ts 文件旁边添加了 js 文件

javascript - 在 Angular 中使用 Rxjs 更新来自不同 Json 源的 Json 中的数据

angular - 使用间隔时如何立即启动 Angular 2 Observable

firebase-realtime-database - 冷热 AngularFire 可观察对象

typescript - typescript 中的 HTMLElement.addEventListener 示例?

javascript - 在 Angular2 中设置基本导入目录

typescript - 如何配置 Webpack 从其 src 文件夹而不是 dist 加载模块?

javascript - Rx 如何在 web 上真正工作(客户端)

angular - 将对象添加到可观察数组的开头