2017-08-28 266 views
3

我想用Angular2編寫Webstorm上的Web應用程序。做這件事我很新。我正在嘗試角網站Angular.IO上的教程。類型'MyObject'中缺少屬性'prototype'

當我嘗試製作人可點擊的列表時,它不起作用。

export class PersonComponent { 
persons = MYPERSONS; 
selectedPersons = Person; 

onSelect(person: Person): void { 
    this.selectedPerson = person; // here is the error on "this.selectedPerson" 
} 
} 

const MYPERSONS: Person[] = [ 
{id:0, firstName: 'First', lastName: 'Firstly', creationData: 1}, 
{id:1, firstName: 'Second', lastName: 'Test', creationData:10}, 
{id:2, firstName: 'Third', lastName: 'Candidate', creationData:5} 
]; 

export class Person { 
id: number; 
firstName: string; 
lastName: string; 
creationData: any; 
} 

我得到以下錯誤:

Type 'Person' is not assignable to type 'typeof Person'. Property 'prototype' is missing in type 'Person'.

是什麼意思?我無法在互聯網上找到這個錯誤。這可能是什麼什麼,我沒有對代碼看到的,因爲我的一些經驗

回答

8

當你寫這樣的:

selectedPersons = Person; 

你指定一個類Person 參考到默認值變量selectedPersons。您可能打算做的是指定selectedPersons class屬性將包含Person類實例。你可以這樣做:

selectedPersons:Person; 
+1

天啊!謝謝。這太微不足道了! – TYL

+1

@TubaYlm,太棒了,別忘了你可以接受我的回答:) –

+2

如果你有上面的錯誤只是檢查=和: – Khuzema