【问题标题】:I'm getting an error in Angular: Property 'catch' does not exist on type 'Observable<Object>'我在 Angular 中遇到错误:“Observable<Object>”类型上不存在属性“catch”
【发布时间】:2019-01-09 22:31:09
【问题描述】:

我正在尝试开发一个使用 python flask API 的 Angular 应用程序。当我开发时,我需要显示一些从后端传递的结果。为此,我开发了一个角度服务。

这是角度服务

import { User } from './user.model';
import { API_URL } from './../theme/env';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';


@Injectable()
export class UserApiService {

    constructor(private http: HttpClient) {
    }

    private static _handleError(err: HttpErrorResponse | any) {
        return Observable.throw(err.messgae || 'Error: Unable to complete request.');
    }

    // GET list of public, future events
    getUser(): Observable<User[]> {
        return this.http
            .get(`${API_URL}/`)
            .catch(UserApiService._handleError);
    }
}

但是当我开发这个服务时,它总是给我这个错误

C:/Users/ChampsoftWK26/Desktop/Projects/KAIROS/KAIROSE 中的错误 FRONTEND/src/app/MainServices/user-api.service.ts (29,14): 属性 类型上不存在“catch” “可观察”

这是我的 package.json 文件,它会帮助你

{
  "name": "ng2-admin",
  "version": "1.0.0",
  "description": "Angular and Bootstrap 4 Admin Template.",
  "author": "Akveo <contact@akveo.com>",
  "homepage": "http://akveo.github.io/ng2-admin/",
  "repository": {
    "type": "git",
    "url": "https://github.com/akveo/ng2-admin.git"
  },
  "bugs": {
    "url": "https://github.com/akveo/ng2-admin/issues"
  },
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "rimraf": "rimraf",
    "wintersmith": "wintersmith",
    "changelog": "standard-changelog",
    "start": "ng serve",
    "start:hmr": "ng serve --hmr",
    "start:aot": "ng serve --aot",
    "start:prod": "ng serve --prod",
    "start:prod:aot": "ng serve --prod --aot",
    "build": "npm run clean:dist && ng build",
    "build:prod": "npm run build -- --prod",
    "build:prod:aot": "npm run build:prod -- --aot",
    "build:ci": "npm run build:prod && npm run build:prod:aot",
    "build:demo": "npm run build:prod:aot -- --base-href \"/ng2-admin/\"",
    "test": "ng test -sr",
    "test:coverage": "npm run clean:coverage && ng test -sr -cc",
    "lint": "ng lint",
    "lint:styles": "stylelint ./src/app/**/*.scss",
    "lint:ci": "npm run lint && npm run lint:styles",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "ng e2e",
    "clean": "npm cache clean && npm run rimraf -- node_modules doc typings coverage dist",
    "clean:dist": "npm run rimraf -- dist",
    "clean:coverage": "npm run rimraf -- coverage",
    "docs:build": "wintersmith build -C docs -o build/ng2-admin",
    "docs:deploy": "npm run docs:build && gh-pages -d docs/build -r git@github.com:akveo/akveo.github.io.git -b master",
    "docs:serve": "wintersmith preview -C docs"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "4.1.3",
    "@angular/common": "4.1.3",
    "@angular/compiler": "4.1.3",
    "@angular/core": "4.1.3",
    "@angular/forms": "4.1.3",
    "@angular/http": "4.1.3",
    "@angular/platform-browser": "4.1.3",
    "@angular/platform-browser-dynamic": "4.1.3",
    "@angular/platform-server": "4.1.3",
    "@angular/router": "4.1.3",
    "@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.26",
    "@ngx-translate/core": "6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "amcharts3": "github:amcharts/amcharts3",
    "ammap3": "github:amcharts/ammap3",
    "angular2-datatable": "0.6.0",
    "animate.css": "3.5.2",
    "bootstrap": "4.0.0-alpha.6",
    "chart.js": "1.1.1",
    "chartist": "0.10.1",
    "chroma-js": "1.3.3",
    "ckeditor": "4.6.2",
    "core-js": "2.4.1",
    "easy-pie-chart": "2.1.7",
    "enhanced-resolve": "^3.3.0",
    "font-awesome": "4.7.0",
    "fullcalendar": "3.3.1",
    "google-maps": "3.2.1",
    "ionicons": "2.0.1",
    "jquery": "3.2.1",
    "jquery-slimscroll": "1.3.8",
    "leaflet": "0.7.7",
    "leaflet-map": "0.2.1",
    "lodash": "4.17.4",
    "ng2-ckeditor": "1.1.6",
    "ng2-completer": "1.3.1",
    "ng2-handsontable": "0.48.0",
    "ng2-slim-loading-bar": "4.0.0",
    "ng2-smart-table": "1.0.3",
    "ng2-tree": "2.0.0-alpha.5",
    "ngx-uploader": "2.2.5",
    "normalize.css": "6.0.0",
    "roboto-fontface": "0.7.0",
    "rxjs": "5.4.0",
    "zone.js": "0.8.11"
  },
  "devDependencies": {
    "@angular/cli": "1.0.4",
    "@angular/compiler-cli": "4.1.3",
    "@types/fullcalendar": "2.7.40",
    "@types/jasmine": "2.5.38",
    "@types/jquery": "2.0.41",
    "@types/jquery.slimscroll": "1.3.30",
    "@types/lodash": "4.14.61",
    "@types/node": "6.0.69",
    "codelyzer": "3.0.1",
    "gh-pages": "0.12.0",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "3.2.0",
    "karma": "1.4.1",
    "karma-chrome-launcher": "2.0.0",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "0.2.0",
    "karma-jasmine": "1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "npm-run-all": "4.0.2",
    "protractor": "5.1.0",
    "rimraf": "2.6.1",
    "standard-changelog": "1.0.1",
    "stylelint": "7.10.1",
    "ts-node": "2.1.2",
    "tslint": "5.2.0",
    "tslint-eslint-rules": "4.0.0",
    "tslint-language-service": "0.9.6",
    "typescript": "2.3.2",
    "typogr": "0.6.6",
    "underscore": "1.8.3",
    "wintersmith": "2.2.5",
    "wintersmith-sassy": "1.1.0"
  }
}

我已经尝试过堆栈溢出中解释的这些问题

Angular 6: Property 'catch' does not exist on type 'Observable<Response>'?

但我没有办法解决这个问题。帮我解决这个问题。

【问题讨论】:

  • 你用的是哪个angular版本和rxjs版本?
  • 我正在使用 "rxjs": "5.4.0",
  • 试试import { Observable } from 'rxjs/Observable';

标签: angular typescript angular-observable


【解决方案1】:
Can you please try this code.

// GET list of public, future events
    getUser(): Observable<User[]> {
        return this.http
            .get(`${API_URL}/`)
            .catch(() => this._handleError());
    }

【讨论】:

  • 他的代码没有错,除了handleErrorthis 的上下文不会引用该类。另一方面,您的代码是错误的。
  • 已更新。为什么使用 _handleError() 的静态方法?
  • 有点好,方法最初来自UserApiService,而不是来自this
  • 虽然此代码可以回答问题,但提供有关 如何为什么 解决问题的附加上下文将提高​​答案的长期价值。
【解决方案2】:

尝试使用最新版本的 Rx:

import { catchError } from 'rxjs/operators';

return this.http
  .get(`${API_URL}/`)
  .pipe(catchError(UserApiService._handleError));

【讨论】:

  • 而且您的标题具有误导性:它让人们相信您使用的是 Angular 6。考虑在标题中删除 CLI 的版本,而将 Angular 和 RxJS 的版本放入其中。
  • 现在它给了我这个,
  • ERROR in C:/Users/ChampsoftWK26/Desktop/Projects/KAIROS/KAIROSE FRONTEND/src/app/MainServices/user-api.service.ts (27,9): Type 'Observable&lt;any&gt;' is not assignable to type 'Observable&lt;User[]&gt;'. Property 'source' is protected but type 'Observable&lt;T&gt;' is not a class derived from 'Observable&lt;T&gt;'.
  • 那是因为你没有输入你的电话:return this.http.get&lt;User[]&gt;(...)
  • getUser(): Observable&lt;User[]&gt;
猜你喜欢
  • 2018-11-30
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 2018-06-25
  • 2020-07-13
  • 2019-03-16
  • 2019-04-06
相关资源
最近更新 更多