How to use rxjs statements inside a TypeScript application

I have a Typescript application and I want to use rxjs.

I did:

npm install --save-dev rxjs

      

then in the main.ts file:

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));
}

      

I am getting compile error:

Module not found: Error: Cannot resolve module 'rxjs/add/observable/from'

      

If you need: here is my gulpfile using Webpack

Do you know what I'm missing? Thanks guys,

0


source to share


1 answer


I checked yours webpack.config.js

where you missed to add resolvable extensions.

The webpack says:



An array of extensions to be used to resolve modules. For example, to detect CoffeeScript files, your array must contain the string ". Coffee".

Default: [".webpack.js", ".web.js", ".js"]

IMPORTANT: Setting this parameter overrides the default, which means webpack will no longer try to resolve modules using extensions. If you need the modules that were needed with them (e.g. require ('./somefile.ext')) to resolve correctly, you must include an empty string in your array. Likewise, if you want modules that were required without extensions (eg require ("underscore")) to be resolved to files with ".js" extensions, you must include ".js" in your array.

Since you are overriding the default extension options for the default webpack solution, you must tell the webpack that please allow modules with extensions ['.js', '.ts', '']

.

+3


source







All Articles