New to RxJS, range is not a function

点点圈 提交于 2020-01-02 05:41:11

问题


I'm trying to create a simple TypeScript file to use RxJS. Here is what I did:

"npm install rxjs", referenced "traceur & systemjs" in my "index.html" and created a "test.ts" file with this:

import {Observable} from 'rxjs/Rx';

Observable.range(1, 2, 3, 4, 5)
    .map(x => x * 2)
    .filter(x => x > 5)
    .subscribe(
        x => console.log(x),
        err => console.log('Error'),
        ok => console.log('No error')
    );

I'm configuring systemjs like this:

System.config({
    defaultJSExtensions: true,
    map: {
        'rxjs': 'node_modules/rxjs'
    }
});

System.import('test');

According to the documentation, the "Observable" type should contains a "range" method but apparently, it does only contain the "Create" one... Therefore, I got the error "Error: TypeError: Observable_1.Observable.fromArray is not a function".

I'm quite new in all this "TypeScript/systemjs/rxjs" stuff so I'm sorry if my question is stupid :-D


回答1:


You need to also import range:

import 'rxjs/add/observable/range';



回答2:


For rxjs 6.*

import { range } from "rxjs";

const observable = range(0, 4);


来源:https://stackoverflow.com/questions/36167740/new-to-rxjs-range-is-not-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!