Map doesn't exist on Observable<Object> with angular 6.0.0 and rxjs 6.1.0

前端 未结 7 708
情歌与酒
情歌与酒 2020-12-29 07:46

Hi I\'m trying to follow a tutorial on angular but the tutorial was made in September. I believe the person used angular-cli 1.3.2. I\'m not sure which version of rxjs he wa

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 08:02

    pipe is a method on Observable which is used for composing operators

    Here is the way how you can use the new method pipe() in Version 6:

    loadProducts() {
        return this.http.get("/api/products").
            pipe(
               map((data: any[]) => {
                 this.products = data;
                 return true;
               }), catchError( error => {
                 return throwError( 'Something went wrong!' )
               });
            )
    }
    

    Keep in mind that with Version 6 you should now use catchError and throwError instead of:catch and throw. Here is the correct import with Version 6:

    import { Observable, of, throwError, ...} from "rxjs"

    import { map, catchError, ...} from "rxjs/operatros"

提交回复
热议问题