2016-04-16 53 views
13

好吧,這是我的第一天使用打字機做一些Angular 2,我嘗試做一個簡單的getter和setter服務。Typescript getter和setter錯誤

import {Injectable} from "angular2/core"; 

@Injectable() 
export class TodoService { 

    private _todos:Array = []; 

    get todos():Array { 
    return this._todos; 
    } 

    set todos(value:Array) { 
    this._todos = value; 
    } 

} 

任何人都可以解釋爲什麼打字稿編譯器拋出下面的錯誤,因爲我認爲它應該沒問題。

ERROR in [default] /Users/testguy/WebstormProjects/angular2-seed/src/app/services/todo-service.ts:6:17 
Generic type 'Array<T>' requires 1 type argument(s). 

ERROR in [default] /Users/testguy/WebstormProjects/angular2-seed/src/app/services/todo-service.ts:8:14 
Generic type 'Array<T>' requires 1 type argument(s). 

ERROR in [default] /Users/testguy/WebstormProjects/angular2-seed/src/app/services/todo-service.ts:12:18 
Generic type 'Array<T>' requires 1 type argument(s). 

回答

10

你應該真正需要的,何況你要哪一種Array而定義它,MyClass可以string/number(數據類型)

代碼

export class TodoService { 

    private _todos:Array<MyClass> = []; 

    get todos():Array<MyClass> { 
    return this._todos; 
    } 

    set todos(value:Array<MyClass>) { 
    this._todos = value; 
    } 

} 
+0

如果有任何其他人絆倒因此您需要將類型定義爲數組,然後在<>中創建一個類,該類是描述數組項的結構的模型。 –