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

前端之家收集整理的这篇文章主要介绍了javascript – 错误TS2339:带有Angular2的类型’HeroService’上不存在属性’handleError’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,伙计们,抱歉这个问题,因为我知道有几个已经有关于编写的Typescript,但我遇到了这个问题,我无法连接点.

我正在尝试按照Angular2 Tour of Heroes应用程序,并尝试用the chapter on Http完成它,但我的hero.service.ts文件输出错误

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

我从这里的其他问题中了解到,这很可能与Typescript的配置方式有关,但似乎没有这个问题的一致原因.

我不熟悉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));
  }
}

我有点困惑的是,错误消息说它无法在’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类的底部并且都工作了.希望它也适合你:).

原文链接:https://www.f2er.com/js/156180.html

猜你在找的JavaScript相关文章