gpt4 book ai didi

angular - 错误 TS2339 : Property 'handleError' does not exist on type 'HeroService' with Angular

转载 作者:太空狗 更新时间:2023-10-29 17:16:11 24 4
gpt4 key购买 nike

好的,伙计们,很抱歉这个问题,因为我知道编译时已经有几个关于 Typescript 的问题,但我遇到了这个问题的味道,我无法将这些点联系起来。

我正在尝试关注 Angular Tour of Heroes 应用程序,并尝试以 the chapter on Http 结束它,但是我的 hero.service.ts 文件输出了这个错误:

error TS2339: Property 'handleError' does not exist on type 'HeroService'

我从这里的其他问题知道,这很可能与 Typescript 的配置方式有关,但似乎没有一致的原因导致此问题。

我对 Typescript(或 Angular)的特性不够熟悉,无法自行解决问题。

我的代码现在是这样的:

代码:

import { Injectable }      from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/toPromise';

import { Hero } from './hero';


@Injectable()
export class HeroService {
private heroesUrl: 'app/heroes'; //URL to web api

constructor(private http: Http) { }

getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}

getHeroesSlowly(): Promise<Hero[]> {
return new Promise<Hero[]>(resolve =>
setTimeout(resolve, 2000))
.then(() => this.getHeroes());
}

getHero(id: number): Promise<Hero> {
return this.getHeroes()
.then(heroes => heroes.find(hero => hero.id === id));
}
}

错误消息说它无法找到“HeroService”上的属性,这是一个在程序中创建的类,而不是 Typescript 本身的一部分,这让我有些困惑。

我的 systemjs.config.js 文件如下所示:

SYSTEMJS.CONFIG:

/**
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);

我的 package.json 文件是这样的:

PACKAGE.JSON:

{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"@angular/upgrade": "2.0.0",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.27",
"zone.js": "^0.6.23",
"angular2-in-memory-web-api": "0.0.20",
"bootstrap": "^3.3.6"
},
"devDependencies": {
"concurrently": "^2.2.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.2",
"typings":"^1.3.2"
}
}

如果这个问题与我没有看到的这个问题的某个其他版本重复,请随时指导我。

谢谢。

最佳答案

在 ToH 教程中,在 HTTP 章节的“错误处理”部分下,它有点“提到”。但是您并不清楚是否需要将此代码部分添加到您的“hero.service.ts”文件中(至少在您倾向于不完全阅读文本的情况下):

private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}

我只是将它添加到 HeroService 类的底部并且一切正常。希望它也对你有用 :)。

关于angular - 错误 TS2339 : Property 'handleError' does not exist on type 'HeroService' with Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com