2016-10-10 69 views
1

我工作過的英雄教程遊Angular2並通過http section從我hero.service.ts文件的工作,最近得到這個錯誤問題:TS1005誤差Angular2 - 與設置

錯誤:

app/hero.service.ts(12,23): error TS1005: '(' expected. 
app/hero.service.ts(12,30): error TS1005: '=' expected. 

通過運行此代碼:

代碼:

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 headers: new Headers({'Content-Type': 'application/json'}); 
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)); 
} 

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

create(name: string): Promise<Hero> { 
return this.http 
    .post(this.heroesUrl, JSON.stringify({name: name}), {headers:  
this.headers}) 
    .toPromise() 
    .then(res => res.json().data) 
    .catch(this.handleError); 
} 

delete(id: number): Promise<void> { 
const url = `${this.heroesUrl}/${id}`; 
return this.http.delete(url, {headers: this.headers}) 
    .toPromise() 
    .then(() => null) 
    .catch(this.handleError); 
} 

update(hero: Hero): Promise<Hero> { 
const url = `${this.heroesUrl}/${hero.id}`; 
return this.http 
    .put(url, JSON.stringify(hero), {headers: this.headers}) 
    .toPromise() 
    .then(() => hero) 
    .catch(this.handleError); 
} 
} 

起初我以爲這是一個模糊的問題,但經過多次檢查,沒有看到答案,我複製並粘貼了答案,他們的plunkr仍然收到相同的錯誤信息。

所以大概是我的TypeScript實現沒有正確實現。

我的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" 
} 
} 

有誰知道這個問題可能是什麼?

我的操作系統是Windows10。

謝謝。

+0

是這一切的hero.service.ts的?如果沒有,請更新並添加hero.service.ts文件中的所有代碼。 – Sefa

+0

你確定你複製並粘貼了嗎? –

+0

我剛剛添加了整個文件。在我只顯示到第13行之前,發生了錯誤。 –

回答

1

我想這應該是

private headers = new Headers({'Content-Type': 'application/json'}); 
private heroesUrl = 'app/heroes'; //URL to web api 

或者

private headers: Headers = new Headers({'Content-Type': 'application/json'}); 
private heroesUrl: string = 'app/heroes'; //URL to web api