【问题标题】:Is there a performance difference between <ng-container> and <ng-template>?<ng-container> 和 <ng-template> 之间有性能差异吗?
【发布时间】:2018-09-09 12:39:56
【问题描述】:

我对@9​​87654322@ 和&lt;ng-template&gt; 有疑问。在我们的代码中使用哪个更好。我已经阅读了关于这两个标签与 SO 上here 之间区别的答案。它说 Angular 编译器通过转换去糖(让它有点复杂)

 `<div *ngFor="let person of persons"></div>`

到这里

<ng-template ngFor let-person="$implicit" [ngForOf]="persons">
   <div>...</div>
 </ng-template>

所以我担心&lt;ng-template&gt;&lt;ng-container&gt; 相比会影响浏览器中的渲染性能,并且应该优先考虑这两个中的哪一个?

【问题讨论】:

    标签: angular angular2-template ng-template ng-container


    【解决方案1】:

    如果您使用的是 AoT 编译,则根本没有区别,因为编译会发生......远远提前。即使使用 JiT 编译,您也不应该真正担心它,除非您要渲染数千个元素。

    想到的一些优化是:

    【讨论】:

    • 实际上,它可能会渲染多达 1000 个元素。那么偏好呢。应该使用哪个?
    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多