![](/img/trans.png)
[英]Using @input property in child component to call service in angular 2
[英]Property using child component in Angular?
如何決定何時在父內部使用子角度組件?
它應該是一個實體,例如課程列表和添加課程。
我想知道什么時候應該使用子組件,什么時候父子之間存在真正的關系?
讓我們假設我們有里面有按鈕的產品卡:
<app-product-card>
<!-- Product information here -->
<app-product-buttons></app-product-buttons>
</app-product-card>
按鈕app-product-buttons
有 clic 事件,顯示購買此產品的用戶列表。
我應該在里面插入<app-users-producst-buy></app-users-producst-buy>
:
<app-product-card>
<!-- Product information here -->
<app-product-buttons [product]="product"></app-product-buttons>
<app-users-producst-buy></app-users-producst-buy>
</app-product-card>
或者在按鈕組件中作為子項:
<app-product-card>
<!-- Product information here -->
<app-product-buttons>
<app-users-producst-buy [product]="product"></app-users-producst-buy>
</app-product-buttons>
</app-product-card>
如何在這種情況下做出正確的決定? 還是應該使用動態組件?
根據我對 Angular 框架的經驗,這個決定並不總是像你所說的那樣容易。
此外,您可以應用這種規則:
1)模塊化:當我要設計的子組件在項目中出現不止一個地方時,我總是使用子組件。
2)可維護性:越簡單,越容易維護。 當一個組件往往很大,並且可以識別多個子組件時,讓我們拆分它。
3) DRY(不要重復自己):考慮小型的、專門的組件(當然,不要過多地考慮)可能是保持小而功能強大的代碼庫的關鍵。
我希望這些要點可以幫助您做出決定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.