Angular 5 to 6 Upgrade: Property 'map' does not exist on type Observable

前端 未结 4 1892
误落风尘
误落风尘 2020-12-03 10:18

Ive upgraded my angular application from version 5 to 6 and im getting this error from the following code.

  const request = this.evidenceService.get().map((         


        
相关标签:
4条回答
  • 2020-12-03 10:50

    according to https://www.academind.com/learn/javascript/rxjs-6-what-changed/

    in past

    import 'rxjs/add/operator/map'
    
    myObservable
      .map(data => data * 2)
      .subscribe(...);
    

    now

    import { map } from 'rxjs/operators';
    
    myObservable
      .pipe(map(data => data * 2))
      .subscribe(...);
    
    0 讨论(0)
  • 2020-12-03 10:56

    Use

    .pipe(map((res) => res.data))
    

    instead of

    .map((res) => res.data)
    
    0 讨论(0)
  • 2020-12-03 10:58

    This solved my problem here is the code:

    import { map } from "rxjs/operators";
    

    **********************************************Example**Below**************************************

    getPosts(){
    this.http.get('http://jsonplaceholder.typicode.com/posts')
    .pipe(map(res => res.json()));
    }
    }
    
    0 讨论(0)
  • 2020-12-03 11:02

    Operator chaining has been transitioned to the use of .pipe() in RXJS v6, you should follow the recommended migration path for RXJS. Additionally, the catch operator has been renamed to catchError.

    Here is how it should be done now:

    const request = this.evidenceService.get().pipe(
        map((res) => res.data)),
        catchError(error => Observable.of(null))
      );
    
    0 讨论(0)
提交回复
热议问题