angular - Angular:在带有 ngTemplateOutlet 的 ng-template 上下文的组件模板中使用对象文字的性能影响
问题描述
将上下文传递给 ng-template 时,在模板中使用对象字面量对性能有何影响?
<ng-container *ngTemplateOutlet="template;context:{ field: value };"></ng-container>
相对于:
<ng-container *ngTemplateOutlet="template;context:context;"></ng-container>
context = { field: value }
变更检测是否会在第一种情况下更频繁地运行,因为它(可能)创建了一个新对象?
/**
* We need to re-create existing embedded view if:
* - templateRef has changed
* - context has changes
*
* We mark context object as changed when the corresponding object
* shape changes (new properties are added or existing properties are removed).
* In other words we consider context with the same properties as "the same" even
* if object reference changes (see https://github.com/angular/angular/issues/13407).
*/
private _shouldRecreateView(changes: SimpleChanges): boolean {
const ctxChange = changes['ngTemplateOutletContext'];
return !!changes['ngTemplateOutlet'] || (ctxChange && this._hasContextShapeChanged(ctxChange));
}
从这一点来看,我会说它确实有所作为,因为如果"ngTemplateOutletContext"
在changes
. 如果它在模板中被定义为对象字面量,它就会一直存在。这假设每个周期都会创建一个新对象。这个假设是错误的吗?
解决方案
推荐阅读
- database - 如何从给定表中检索后半部分记录
- node.js - 尝试将新变量推送到数组时“userIDs.push 不是函数”
- node.js - NodeJs error: Cannot GET /admin when using router
- django - 导出pdf文件
- vba - 在幻灯片上重新定位形状和文本框
- android - Android 开发者可以查看我的 Google Play 帐户吗?
- c# - 测试用例的空 int 输入
- java - 如何将存储过程的结果映射到spring-boot/hibernate中的实体
- r - 使用混淆矩阵找到截止值?
- firebase - 有什么方法可以使用事件参数来触发 Firebase 中的应用内消息?