简体   繁体   中英

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!") 
     ) 
}

Import from directly

import { from } from 'rxjs';

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

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM