我正在試圖定義一個名爲NavItem
的TypeScript類,我想持有一個標題和URL。我覺得,如果我定義這個類和使用對象符號{title: "foo", url: "bar"}
它完美,但只要我添加了一個構造函數(即使我不立即使用它),它完全打破實例吧:爲什麼添加構造函數在這裏打破Angular 2?
import {Component, Input} from 'angular2/core'
@Component({
selector: 'nav-item',
templateUrl: './views/navitem.html',
})
export class NavItem {
@Input() title: String = "default title";
@Input() url: String;
// if I comment the following out it works fine:
constructor(inTitle: String, inUrl: String) {
this.title = inTitle;
this.url = inUrl;
}
}
如果我把這個構造函數放入我的頁面中:
例外:String沒有提供者! (NavItem - >字符串)在[在TopNav @ 2 navItems:11]
我明白了。我認爲我的問題是我需要分割我的模型和視圖。我將'NavItem'視爲模型和視圖(具有屬性的視圖),而這對於Angular來說似乎是一個壞主意。 – devios1