错误TS2339:带有Angular2的类型'HeroService'上不存在属性'handleError'

Jon*_*tel 7 typescript angular

好吧,伙计们,抱歉这个问题,因为我知道有几个已经有关于编写的Typescript,但我遇到了这个问题,我无法连接点.

我想跟随英雄的应用程序的Angular2巡回赛,并正尝试与完成它基于HTTP的一章,但我的hero.service.ts文件输出这个错误:

error TS2339: Property 'handleError' does not exist on type 'HeroService'
Run Code Online (Sandbox Code Playgroud)

我从这里,这最有可能有事情做与打字稿是如何配置的其他问题都知道,但似乎并没有成为这个问题的一致原因.

我不熟悉Typescript(或Angular2)的特性来解决我自己的问题.

我现在的代码如下所示:

码:

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));
  }
}
Run Code Online (Sandbox Code Playgroud)

我有点困惑的是,错误消息说它无法在'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);
Run Code Online (Sandbox Code Playgroud)

我的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"
}
}
Run Code Online (Sandbox Code Playgroud)

如果这个问题与这个问题的其他一些版本重复,那么我没有看到这些问题随意引导我.

谢谢.

小智 19

在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);
 }
Run Code Online (Sandbox Code Playgroud)

我只是将它添加到HeroService类的底部并且都工作了.希望它也适合你:).