简体   繁体   English

在 Angular 中使用子组件的属性?

[英]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.

相关问题 在子组件中使用@input属性调用Angular 2中的服务 - Using @input property in child component to call service in angular 2 Angular2中的子级属性更改时更新父级属性(使用路由出口加载的子级组件) - Update Parent Property When Child Property Changes in Angular2 ( Child component loaded using router-outlet) 如何使用 Angular 9 中的“延迟加载”使用子组件的事件属性绑定父组件 - How to bind parent component using child component's event property using `lazy loading` in Angular 9 如何在Angular 5中设置child(或nth child)组件的child的属性 - How to set property of child of child(or nth child) component in Angular 5 如何在角度4中将值分配给子组件属性? - How to assign the values to the child component property in angular 4? 角度2:父子组件属性绑定 - Angular 2: Parent-Child Component Property Binding 使用Angular 7无法在子组件中访问子行 - The child row is not accessible in child component using Angular 7 父组件的角度5设置子组件属性 - angular 5 setting child component property from parent component angular 父组件属性更新不运行子组件中的方法 - angular parent component property update does not run methods in child component 从Angular 2中的子组件更新父组件属性 - Update parent component property from child component in Angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM