我试图让familliar与角2的ChangeDetectionStrategy.OnPush性能提升(如解释在这里).但我这里有古玩的案例.
我有父母AppComponent:
@Component({
selector: 'my-app',
template: `<h1>
<app-literals [title]="title" [dTitle]="dTitle"></app-literals>
<input [value]="title.name"/>
</h1>
`
})
export class AppComponent implements OnInit {
title = { name: 'original' };
dTitle = { name: "original" };
constructor(private changeDetectorRef : ChangeDetectorRef) {
}
ngOnInit(): void {
setTimeout(() => {
alert("About to change");
this.title.name = "changed";
this.dTitle = { name: "changed" };
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
和子LiteralsComponent组件:
@Component({
selector: 'app-literals',
template: ` {{title.name}}
{{dTitle.name}}`,
changeDetection: ChangeDetectionStrategy.OnPush
}) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将ListPicker与对象数组一起使用,并且使用所有元素显示[object Object]的标签来呈现列表。
我想指定哪个属性用作列表选择器的“标签”。
不幸的是,Nativescript ListPicker仅接受一个字符串数组,并且我不能使用我的Object数组,因为标签将调用toString()
我发现了基于以下的替代解决方案:https : //github.com/NativeScript/NativeScript/issues/1677
购买我的应用程序时使用的是page-router-outlet,并且不使用元素,因此我无法使用上述建议的方法。在这种情况下,有没有可能将ListPicker与对象数组一起使用,或者有任何不依赖Page元素加载事件的解决方法?
关于ChangeDetectorRef,我已经知道——
detectChanges实际触发更改检测,同时 -markForCheck- 组件的实际更改检测未 安排,但在将来何时发生(作为当前或下一个 CD 周期的一部分)
看着markForCheck- 如果它没有被安排,那么它什么时候运行?显然在 Observables 回调、异步回调和 setTimout 和事件之后。
该文档有一个带有OnPush策略的组件
@Component({
selector: 'cmp',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `Number of ticks: {{numberOfTicks}}`
})
class Cmp {
numberOfTicks = 0;
constructor(private ref: ChangeDetectorRef) {
setInterval(() => {
this.numberOfTicks++;
// the following is required, otherwise the view will not be updated
this.ref.markForCheck();
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
题:
如果它标记为检查其祖先,则为下一个周期,那么谁运行当前周期?(它是上一个 setTimeout 调用吗?)
因为这段代码显示每一秒都被另一个替换——换句话说,每一秒都有一个变化检测(?!)。
通过 POV 的步骤实际上发生了什么?
我已经阅读了一些关于Change DetectionAngular 的好文章,并且对它是什么以及它是如何工作的有了一定的了解。但是到目前为止我读过的每篇文章都只关注组件上Change Detection何时发生某些事件或某些input属性更改时等。我还没有找到任何文章关注route更改时会发生什么?Change Detection在这种情况下如何工作?此外,Angular 是一次性将所有 HTML/DOM 更新推送到浏览器,还是在发现任何 DOM 更新后立即不断向浏览器提供信息?