node.js - Angular 6 :Property catch does not exist

标签 node.js angular

我是 Angular 的新手,并且对其有基本了解。我想学习 HttpClient,这样我就可以创建一个 json 文件而不是真正的服务器。我创建了一个服务并导入了 HttpClient:

服务.ts

import { Injectable } from '@angular/core';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {IEmployee} from "../../../service/src/app/employee";
import {Observable} from "rxjs/index";
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

在我的类(class)EmployeeService我创建了一种从 json 文件获取数据的方法:

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {

  private _url: string = "/assets/data/employees.json";

  constructor(private http:HttpClient) { }

  getEmployee():Observable<IEmployee[]> {
    return this.http.get<IEmployee[]>(this._url)
      .catch(this.errorHandler);
  }

  errorHandler(error: HttpErrorResponse) {
    return Observable.throw(error.message || "Server Error")
  }
}

但是在 getEmployee 方法中我遇到了这些错误:

ERROR in ./src/app/employee.service.ts
Module not found: Error: Can't resolve 'rxjs/add/observable/throw' in 'E:\Tutorial\NodeJS\WebstormProjects\Angular\http\src\app'
ERROR in ./src/app/employee.service.ts
Module not found: Error: Can't resolve 'rxjs/add/operator/catch' in 'E:\Tutorial\NodeJS\WebstormProjects\Angular\http\src\app'
i 「wdm」: Failed to compile.

如你所见,我已经导入了 throwcatch运算符,但我不知道为什么我不断收到错误。

另一个问题是,下面throw method由于已弃用(Deprecated symbol used, consult docs for better alternative)而出现一行! 有什么替代方案吗?

Angular CLI: 6.0.1
Node: 10.0.0
OS: win32 x64
Angular:
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.6.1
@angular-devkit/core         0.6.1
@angular-devkit/schematics   0.6.1
@schematics/angular          0.6.1
@schematics/update           0.6.1
rxjs                         6.1.0
typescript                   2.7.2

******************** 编辑****************

I want to use builtin Observable in Angular yet and i do not want to use RXJS third party lib for angular.

这是我的 Node 模块 rx 文件夹,您可以在其中看到可观察的文件。

enter image description here

node_modules\rxjs\operator文件夹中有throwcatch文件.. 但为什么它要将这些文件搜索到 E:\Tutorial\NodeJS\WebstormProjects\Angular\http\src\app哪个文件夹出错了?

最佳答案

我通过安装解决了我的问题:

npm install --save rxjs@6 rxjs-compat@6

并将此路径用于 rxjs:

import {Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, throwError} from 'rxjs';
import {catchError} from "rxjs/internal/operators";

似乎 catch 在 Angular 6 处被贬低,所以为了我使用了如下所示的 catchError :

  getEmployee():Observable<IEmployee[]> {
    return this.http.get<IEmployee[]>(this._url)
      .pipe(
        catchError(this.errorHandler));
  }

  errorHandler(error: HttpErrorResponse) {
    return throwError(error.message || "Server Error")
  }

现在所有错误都消失了:-)

关于node.js - Angular 6 :Property catch does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50409418/

相关文章:

javascript - 如何通过 CDN 在我的网站中使用 puppeteer

node.js - 尝试连接到 nodejs 中的 postgresql 池时如何修复 ECONNREFUSED 错误

javascript - discord.js v14 中的 setPresence 事件类型只能设置为 "PLAYING"

angular - 通过函数调用使用 routerLink

angular - 银格复位过滤器

node.js - Angular 5 socket.io 未收到其他组件中的更新

javascript - 如果静态文件不匹配,则仅匹配 GET 和 POST 路由?

node.js - 如何使用 docker-compose 运行 Express 和 Mongo

angular - 没有项目支持 'extract-i18n' 目标

angular2 应用程序容器化