如何在 TypeScript 应用程序中使用 rxjs 运算符 [英] How to use rxjs operators inside a TypeScript app
问题描述
我有一个非常 Typescript 的应用程序,我想使用 rxjs.
I have a very Typescript app and I want to use rxjs.
我做到了:
npm install --save-dev rxjs
然后在文件 main.ts 中:
then in the file main.ts:
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';
import 'rxjs/add/operator/filter';
function test() {
Observable.from([0,1,2,3,4])
.filter((x) => x >= 2)
.subscribe((x) => console.log(x));
}
我收到一个编译错误:
Module not found: Error: Cannot resolve module 'rxjs/add/observable/from'
如果您需要:这是我的 gulpfile,使用 Webpack
In case you need: here is my gulpfile, using Webpack
你知道我错过了什么吗?谢谢各位
Do you know what I'm missing ? Thank you guys
推荐答案
我正在检查您的 webpack.config.js
,您错过了添加可解析扩展的位置.
I was checking your webpack.config.js
, where you missed to add the resolvable extensions.
webpack 说,
应用于解析模块的扩展数组.为了例如,为了发现 CoffeeScript 文件,您的数组应该包含字符串.coffee".
An array of extensions that should be used to resolve modules. For example, in order to discover CoffeeScript files, your array should contain the string ".coffee".
默认:[".webpack.js", ".web.js", ".js"]
Default: [".webpack.js", ".web.js", ".js"]
重要提示:设置此选项将覆盖默认值,这意味着webpack 将不再尝试使用默认值解析模块扩展名.如果您想要模块所需的扩展名(例如 require('./somefile.ext')) 正确解析,您必须在数组中包含一个空字符串.同样,如果你想不需要扩展的模块(例如require('underscore')) 被解析为带有.js"扩展名的文件,您必须在数组中包含.js".
IMPORTANT: Setting this option will override the default, meaning that webpack will no longer try to resolve modules using the default extensions. If you want modules that were required with their extension (e.g. require('./somefile.ext')) to be properly resolved, you must include an empty string in your array. Similarly, if you want modules that were required without extensions (e.g. require('underscore')) to be resolved to files with ".js" extensions, you must include ".js" in your array.
由于您覆盖了 webpack 的默认解析扩展选项,您必须告诉 webpack,请解析具有 ['.js', '.ts', '']
扩展的模块.
Since you are overriding the webpack's default resolve extension options, you have to tell the webpack that, please resolve the modules which has ['.js', '.ts', '']
extensions.
这篇关于如何在 TypeScript 应用程序中使用 rxjs 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!