[英]What is the best approach to render data in Angular?
I have multiple post data inside an array.我在一个数组中有多个帖子数据。 I want to render each post.
我想渲染每个帖子。 There are two approaches to render :
有两种渲染方法:
1: create a reusable component and call it multiple times inside *ngFor 1:创建一个可复用的组件并在*ngFor内部多次调用
<div *ngFor="let post of posts">
<render-post [post]="post"><render-post>
</div>
2: create a component and pass the array inside the component 2:创建一个组件并在组件内部传递数组
<div>
<render-post [posts]="posts"></render-post>
</div>
The more you break into small pieces, the better.你打成的小块越多越好。
I would go with the first one all day.我会整天和第一个一起去。 It makes echo to Single Responsibility Principle, each component must have a unique responsability and do it well.
它呼应了单一职责原则,每个组件都必须有唯一的职责并做好。 The RenderPostComponent must be the way to render one post, and it must do it well.
RenderPostComponent 必须是渲染一篇文章的方式,而且它必须做得很好。 You can still create another component, parent of the previous one, that can be RenderPostListComponent which will contain your
*ngFor
and will get the array in parameter.您仍然可以创建另一个组件,即前一个组件的父组件,它可以是 RenderPostListComponent,它将包含您的
*ngFor
并在参数中获取数组。
So your final solution is a mix of both solution.所以你的最终解决方案是两种解决方案的混合。
<div *ngFor="let post of posts">
<render-post [post]="post"><render-post>
</div>
<div>
<render-post-list [posts]="posts"></render-post-list>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.