欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Property 'from' does not exist on type 'typeof Observable', angular 6 ? 有大用 有大大用

Asked 
Active 10 months ago    
Viewed 6k times
5

I updated my angular 5.2.10 project to angular 6.
I did step by step https://update.angular.io/, everything is OK unless Observable.from
In a service I used Observable.from(this.user) as following:

import { Observable } from 'rxjs/Observable';
...
Observable.from(this.users)// this.users is an array
                   

It was OK, but in angular 6 the following error occurred

Property 'from' does not exist on type 'typeof Observable'
                   

I changed it as follows

import { Observable, from } from 'rxjs';
                   

But no change and error occurred again!

                      

                               
  • 1                                
    you can also fix these upgrade issues using a TSLint rules for RxJs: npm i -g rxjs-tslint rxjs-5-to-6-migrate -p tsconfig.json – Mobiletainment Jun 14 '18 at 21:49                                 
add a comment                
               

3 Answers  正确答案                


               


                   
               
13

In rxjs@6 you can use from as standalone function:

import { from } from 'rxjs';
...

from(this.users);
                       

or

import { from as observableFrom } from 'rxjs';
...

observableFrom(this.users);
                       

See also migration to rxjs6 guide

improve this answer                                
edited May 5 '18 at 6:34                                

add a comment                    
               
1

Without modifying the existing code, still you will be able to run just by installing "rxjs-compat" package.

npm install rxjs-compat --save
                   
improve this answer                                

add a comment                    
               
0

This is changed from previous rxjs versions to rxjs6. (RxJS v5.x to v6 Update Guide)

Before rxjs 6

import { Observable } from "rxjs";

let numbers = [1, 5, 10];
let source = Observable.from(numbers);
                       

With rxjs 6                        

import { from, Observable } from "rxjs";

let numbers = [1, 5, 10];
let source = from(numbers);
                   

                               
add a comment                    
               

来自  https://stackoverflow.com/questions/50186371/property-from-does-not-exist-on-type-typeof-observable-angular-6   


           


           


           

Asked 
Active 6 months ago                
Viewed 382 times
0

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

                                       
  • have imported operators in your ts file? – TheParam Mar 28 at 6:38                                             
  • 1                                            
    which version of angular rxjs you are using? – TheParam Mar 28 at 6:42                                            
add a comment                            
                   

2 Answers   

正确答案                            

activeoldestvotes                                
                   
1

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!


  • Thanks a lot, I have tried, now it's working ! :) – puddlejumper26 Mar 29 at 2:19                                                
  • I'm glad I could help. Please accept the answer so others will get benefit out it – TheParam Mar 29 at 3:34                                                
add a comment                                
                   
3

Import from directly

import { from } from 'rxjs';

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

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


add a comment                                


                   


                   

来自   https://stackoverflow.com/questions/55391507/property-from-does-not-exist-on-type-typeof-observable-ts2339                    

                   

 
普通分类: