在Udemy課程中,我們一直允許組件在組件類中使用@Input()
裝飾器來傳遞數據dy。Angular 2/TypeScript:@ Input/@輸出或輸入/輸出?
在通過ngBook-2閱讀時,我發現使用@Component
裝飾器中的input
屬性還有另一種方法。
THIS類似的問題對SO,一個人回答說:
使用輸入的一個優點是類的用戶只需要看看傳遞給@Component裝飾的配置對象,以找到輸入(和輸出)屬性。
,並期待通過documentation狀態:
無論您使用的輸入/輸出或@輸入/輸出@的結果是相同的,所以在選擇使用哪一個在很大程度上是一種風格決定。
真的,這個最有用的信息取決於你往哪裏看主要是相互矛盾的。
內@Component
@Component({
selector: 'product-image',
inputs: ['product'],
template: `
<img class="product-image" [src]="product.imageUrl">
})
class ProductImage {
product: Product;
}
課內
@Component({
selector: 'product-image',
template: `
<img class="product-image" [src]="product.imageUrl">
})
class ProductImage {
@Input() product: Product;
}
事情我想知道
- 什麼人會更多「最佳實踐」用法?
- 什麼時候你會用另一個?
- 有什麼不同嗎?
還檢查http://stackoverflow.com/questions/40661325/why-is-input-decorator-preferred-over-inputs/40662020#answer-40661361 – yurzui