0

I am importing like this:

import { Observable } from 'rxjs' ;

or

import { Observable } from 'rxjs/Rx' ;

And declared a property like this

ob : Observable<any>;

When I write this.ob.fromarray() in my constructor I'm only getting IntelliSense for subscribe and 2 or 3 other functions, not fromarray which is required.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
blackHawk
  • 6,047
  • 13
  • 57
  • 100

2 Answers2

1

You probably want to import the whole ReactiveXJS package like this:

import 'rxjs/Rx';

On the other hand Observable.fromArray(arr: Array<any>) is a static method - you can't use it on an instance. The documentation though says, that this method is deprecated and one should use Observable.from() now.

Use it like so for instance:

//our root app component
import {Component} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import 'rxjs/Rx';

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div *ngFor="let item of output">{{item}}</div>
  `,
  directives: []
})
export class App {
  ob: Observable<any>;

  someArray: Array<number> = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
  output: Array<number> = [];

  constructor() {
    this.ob = Observable.from(this.someArray);

    this.ob.subscribe((data) => {
      this.output.push(data);
    });
  }
}
Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
-1

You need to import the operators seperatly like this:

import {Observable} from "rxjs/Observable";

import "rxjs/add/observable/interval";
import "rxjs/add/operator/take";
import "rxjs/add/operator/map";
import "rxjs/add/operator/bufferCount"

check this for example

And I think that fromarray() is deprecated. Use from() instead. I hope this can help you.