在 TypeScript 中使用 RxJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36933788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:06:32  来源:igfitidea点击:

Using RxJS with TypeScript

typescriptrxjs

提问by ba0708

I have a problem trying to use RxJS with TypeScript and Node.js. I am using NPM, and I have included rxjs-esversion 5. I am also using Typings, and I have included both es6-shimand rx.all, like so:

我在尝试将 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"
  }
}

Below is my tsconfig.jsonfile.

下面是我的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"
  ]
}

In a TypeScript file, I am trying to do the following:

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

import { Observable } from 'rxjs/Observable';

import { Observable } from 'rxjs/Observable';

When I try to compile the script, I get the following error:

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

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

This happens even if I try to use RxJS within my script or not, so the problem is related to the TypeScript typings.

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

What do I need to install using typings and NPM in order to use RxJS? In particular, I am interested in using Observables. After Googling for hours, I cannot seem to figure out just what I need to make it work. I have tried the following, but without any luck. I have also tried many combinations of rx packages in NPM and typings, but haven't found anything that works. Any help is much appreciated!

为了使用 RxJS,我需要使用 Typings 和 NPM 安装什么?特别是,我对使用 Observables 感兴趣。在谷歌搜索了几个小时后,我似乎无法弄清楚我需要什么才能让它工作。我已经尝试了以下,但没有任何运气。我还尝试了 NPM 和类型中 rx 包的多种组合,但没有找到任何有效的方法。任何帮助深表感谢!

回答by OJ Kwon

RxJS 5 is written in typescript and its type definitions are included by default for published module, you may not need typings for rx.all. Actually those type definitions are written for previous versions of RxJS. Simply uninstall rx.alltype definition and try to import without those.

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

回答by Latin Warrior

The correct way of accomplishing this for Angular(not AngularJS) is by using @types from npm. The command is as follows:

Angular(而不是AngularJS)完成此操作的正确方法是使用 npm 中的 @types。命令如下:

npm install @types/rx --save-dev

For more information, see npm.

有关更多信息,请参阅npm

回答by Rafal Pastuszak

Generally, you don't need to install typings separately, just including rxjs/Rxin your project should work (note the package name):

通常,您不需要单独安装类型,只需rxjs/Rx在您的项目中包含即可(注意包名称):

// installed with `$ npm i rxjs`
import { Observable } from 'rxjs/Rx' // <- rxjs from NPM

Observable
    .of('Hello')
    .map( w => w + ' world!') // Annotated properly: (w: string) => string
    .subscribe(console.log.bind(console)) // 'Hello world!'

My .tsconfig(node)

我的.tsconfig(节点)

Shouldn't be relevant, but feel free to use/compare.

不应该相关,但可以随意使用/比较。

{
    "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "removeComments": false,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "preserveConstEnums": true
    },
    "exclude": [
        "logs",
        "node_modules"
    ]
}

回答by Corey Alix

That typings file depends on Rx:

该类型文件取决于 Rx:

declare module "rx.all" {
    export = Rx;
}

Try installing "rx" as well as "rx.all":

尝试安装“rx”和“rx.all”:

typings install rx --save --ambient

But notice these typings assume you are importing "rx.all" or "rx" so your import will not give you typing info:

但请注意,这些类型假设您正在导入“rx.all”或“rx”,因此您的导入不会为您提供键入信息:

import { Observable } from 'rxjs/Observable';

Can you import from "rx" instead of "rxjs"?

您可以从“rx”而不是“rxjs”导入吗?

import { Observable } from 'rx';

回答by Howard Panton

You can install typings for RXJS by entering the following in your CLI.

您可以通过在 CLI 中输入以下内容来安装 RXJS 的类型。

typings install --global dt~es6-shim --save

This will add the following to your typings.json"es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504"

这会将以下内容添加到您的 typings.json"es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504"

Then add a reference path to the top of your typescript file

然后在打字稿文件的顶部添加一个参考路径

/// <reference path="typings/globals/es6-shim/index.d.ts" />`

or to your tsconfig.json.

或到您的 tsconfig.json。

This will remove any typescript errors.

这将删除任何打字稿错误。

回答by Rick

If you are using TypeScript 2 you can try this.

如果您使用的是 TypeScript 2,您可以试试这个。

typings install npm:rx/ts/rx.all.d.ts --save --global