javascript - Angular 2 屬性註解無法編譯
以下程式碼
import {Attribute, Component, View} from 'angular2/angular2';
@Component({
selector: 'card'
})
@View({
templateUrl: "app/design/card/card.html",
styleUrls: ["app/design/card/card.css"],
directives: []
})
export class Card {
constructor(@Attribute('no-teaser') noTeaser) {
this.active=false
}
}
給我錯誤
Unable to resolve signature of parameter decorator when called as an expression. Supplied parameters do not match any signature of call target.
@Attribute('home')
當將 TypeScript 翻譯成 JavaScript 時。
我正在使用 Alpha 37,來自 DefinitelyTyped 的最新定義,並且我正在使用 atom-typescript(它使用最新的 TypeScript 版本)
答案#1
無法解析作為表達式調用時參數裝飾器的簽名。提供的參數與調用目標的任何簽名都不匹配。
在 Google 搜尋這個錯誤結果是: https://github.com/Microsoft/TypeScript/issues/3661
你可以在那裡看到:
裝飾函數的形式參數目前不是 ES7 提案的一部分。
所以我懷疑 constructor(@Attribute('no-teaser') noTeaser) 在 最新 的 TypeScript 中是 故意 禁止的(可以試試 ntypescript https://github.com/TypeStrong/ntypescript)。如果確實如此,請向 Microsoft/TypeScript 報告作為一個問題。
是否可以告訴 atom-typescript 使用哪個版本的 tsc?
不容易,因為舊版本的 TSC 可能不會與最新版本 API 兼容… 但有這個選項:https://github.com/TypeStrong/atom-typescript/blob/master/docs/faq.md#can-i-use-a-custom-typescript-compiler
答案#2
這是 Angular 中屬性註解定義方式的問題。當我嘗試創建自己的註解時,也遇到了相同的錯誤:
export function Attribute(name: string) {
return function(a, b) {
console.log(a);
console.log(b);
}
}
出現了和你一樣的錯誤,所以當我改成這個時:
export function Attribute(name: string) {
return function(a, b, c) {
console.log(a);
console.log(b);
console.log(c);
}
}
錯誤消失了。看起來你需要向 Angular 專案提交問題。
