[英]Property using child component in Angular?
How to decide when to use child angular component inside parent?如何决定何时在父内部使用子角度组件?
Should it be one entity, for example lessons list and add lesson.它应该是一个实体,例如课程列表和添加课程。
I try to get when I should use child component, when exist real relation between parent and child?我想知道什么时候应该使用子组件,什么时候父子之间存在真正的关系?
Lets assume we have product card with buttons inside:让我们假设我们有里面有按钮的产品卡:
<app-product-card>
<!-- Product information here -->
<app-product-buttons></app-product-buttons>
</app-product-card>
On of button app-product-buttons
has clic event, that shows list of users, who bought this product.按钮
app-product-buttons
有 clic 事件,显示购买此产品的用户列表。
Should I insert <app-users-producst-buy></app-users-producst-buy>
inside:我应该在里面插入
<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>
Or inside as child in buttons component:或者在按钮组件中作为子项:
<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>
How to make a right decicion on this cases?如何在这种情况下做出正确的决定? Or should I use dynamic components?
还是应该使用动态组件?
From my experience with Angular framework, this decision is no always easy as you are saying.根据我对 Angular 框架的经验,这个决定并不总是像你所说的那样容易。
Moreover, you can apply that kind of rules:此外,您可以应用这种规则:
1) Modularity: I always use child component when the child component I am about to design will appear in more than one place in the project. 1)模块化:当我要设计的子组件在项目中出现不止一个地方时,我总是使用子组件。
2) Maintainability: The simpler, the more maintainable. 2)可维护性:越简单,越容易维护。 When a component tends to be huge, and you can identify several sub-components, let's split it.
当一个组件往往很大,并且可以识别多个子组件时,让我们拆分它。
3) DRY (Don't Repeat Yourself): thinking in small, specialised components (without going into the excess, for sure) can be a key to keep a small but very functional code-base. 3) DRY(不要重复自己):考虑小型的、专门的组件(当然,不要过多地考虑)可能是保持小而功能强大的代码库的关键。
I hope these points can help you making decisions.我希望这些要点可以帮助您做出决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.