簡體   English   中英

打字稿getter和setter錯誤

[英]Typescript getter and setter error

好吧,這是我第一天使用打字稿做一些Angular 2,我試着做一個簡單的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;
  }

}

任何人都可以解釋為什么Typescript編譯器拋出以下錯誤,因為我認為它應該沒問題。

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).

在定義它時你真的需要提到你想要的那種ArrayMyClass可以是string / number (數據類型)

export class TodoService {

  private _todos:Array<MyClass> = [];

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

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM