2017-08-12 64 views
0

有人可以告訴我使用我的代碼發生了什麼,它不斷地說'無法編譯'。我發現別人的答案,但我的代碼看起來不錯。仍然不知道根源是什麼。謝謝。Angular Demo中的TypeScript錯誤

的代碼:

import { Component } from '@angular/core'; 

export class Hero { 
    id: number; 
    name: string; 
} 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
    template: `<h1>{{title}}</h1> 
       <h2>{{hero.name}} details!</h2> 
       <div><label>id: </label>{{hero.id}}</div> 
       <div><label>name: </label>{{hero.name}}</div> 
       ` 
}) 

export class AppComponent { 
    title = 'Tour of Heroes'; 
    hero: Hero = { 
    id: 1, 
    name: 'Windstorm' 
    }; 
} 

的錯誤消息:

ERROR in E:/workspace/angular-tour-of-heroes/src/app/app.component.ts (22,1): Unused label. 
ERROR in E:/workspace/angular-tour-of-heroes/src/app/app.component.ts (22,7): Cannot assign to 'Hero' because it is not a variable. 

回答

1
let hero: Hero = new Hero(); 
hero.id = 1; 
hero. name = 'Windstorm'; 
0

我迷惑,我的代碼看起來不錯,並不能找出問題的根源。

然後我在我的代碼中添加一個調試器並嘗試調試它,儘管它可能沒用。但是在我刪除'調試器'後,編譯成功。而且一切看起來都很好。

我還不知道這裏發生了什麼。

我想這是npm start問題。 (該命令以「監視模式」運行TypeScript編譯器,在代碼更改時自動重新編譯)。