找不到模块:错误:无法解析'F:\ Angular\HttpServices\http-services\src\app'中的'rxjs/add/operator/catch'

Ayj*_*yed 9 observable rxjs angular

这是我的代码.

import { injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/catch';

@Injectable({
    providedIn: 'root'
})
export class PostsService {
    private url = 'https://jsonplaceholder.typicode.com/posts';
    constructor(private http : Http ) { }
    getPosts {
        return this.http.get(this.url);
    } 

    deletePost(id) {
        return this.http.get(this.url + '/' + id);
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在我的电脑上执行此代码它正在工作,但没有在笔记本电脑上工作.这看起来很有趣,但这是事实.
这是我项目的结构

小智 16

新版本的rxjs不再支持单个导入,而是尝试:

import { catchError } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)


bru*_*dek 11

在Angular 6中,RXJS的导入已被更改,运算符必须在pipe()中"包装"并在开头没有点写入(是:.map,.retry,.catch; now:map,retry和catchError而不是catch ):

import { catchError, map } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)

并使用管道()

getPosts() {
    // pack in "pipe()"
    return this.http.get(this.url).pipe(
      // eg. "map" without a dot before
      map(data => {
        return data;
      }),
      // "catchError" instead "catch"
      catchError(error => {
        return Observable.throw('Something went wrong ;)');
      })
    );
  }
Run Code Online (Sandbox Code Playgroud)

"我正在我的电脑上执行此代码,但它不能在笔记本电脑上工作" - 在笔记本电脑上你有一个更新版本的角度cli.它使用新的其他导入生成角度6.问候


woo*_*ony 5

如果您是因为初始代码建议的 Udemy 课程来到这里。

进口

import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)

这就是现在的样子。

  deletePost(id){
    return this.http.delete(this.url + '/' + id ).pipe(
       catchError(error => {
         return throwError(() => new Error("..."));
      }));
  }
Run Code Online (Sandbox Code Playgroud)


Jit*_*uja -3

为什么不尝试添加“import 'rxjs/add/operators/catch';” (放入 's' 并使其成为运算符)而不是“import 'rxjs/add/operator/catch';”