关于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 的步骤实际上发生了什么?