puddlejumper26
puddlejumper26

Reputation: 33

Property 'from' does not exist on type 'typeof Observable'.ts(2339)

The following code should have the result:

4

16

Done!

However, the VSC told me that : Property 'from' does not exist on type 'typeof Observable'.ts(2339)

I have no idea how to fix the problem. Could anyone help me please?

/These code should be put into any component.ts file, then npm run start to see the result in the console./

import { Observable } from 'rxjs'; 

constructor() {

    Observable.from([1,2,3,4]).filter(e => e%2==0) 
      .map( e => e*e) 
      .subscribe( e => console.log(e),
       error => console.error(error), 
       () => console.log("Done!") 
     ) 
}

Upvotes: 1

Views: 2140

Answers (2)

TheParam
TheParam

Reputation: 10571

In rxjs version 6+ they removed the from operator from the Observable class. So now you need to import from rxjs and use without appending the Observable class.

component.ts

import { Component } from '@angular/core';
import { from } from 'rxjs';
import { filter, map, } from 'rxjs/operators';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  description!: string;

  constructor() {


    from([1, 2, 3, 4])
      .pipe(
        filter((e: number) => e % 2 == 0),
        map(e => e * e)).subscribe(
          e => console.log(e),
          error => console.error(error),
          () => console.log("Done!")
        )
  }
}

Here is solution on stackblitz

Hope this will help!

Upvotes: 2

Ashish Ranjan
Ashish Ranjan

Reputation: 12970

Import from directly

import { from } from 'rxjs';

from([1,2,3,4])

https://www.learnrxjs.io/operators/creation/from.html

Upvotes: 4

Related Questions