简体   繁体   English

角度轮询 trackBy 不会触发 ngDestroy 但组件已更新

[英]Angular polling trackBy doesn't trigger ngDestroy but the component is updated

In this example https://stackblitz.com/edit/angular-ivy-hptdnu?file=src%2Fapp%2Fgrid%2Fgrid.component.html without trackBy the ngOnDestroy hook is triggered and the data updated I can understand it在这个例子中https://stackblitz.com/edit/angular-ivy-hptdnu?file=src%2Fapp%2Fgrid%2Fgrid.component.html without trackBy ngOnDestroy 钩子被触发并且数据更新我可以理解

but in this other example https://stackblitz.com/edit/angular-ivy-aan45e?file=src%2Fapp%2Fgrid%2Fgrid.component.html with trackBy the ngOnDestroy hook is never triggered and the data is updated without any problem but I don't really understand why!但在另一个例子中https://stackblitz.com/edit/angular-ivy-aan45e?file=src%2Fapp%2Fgrid%2Fgrid.component.html with trackBy ngOnDestroy 钩子永远不会被触发,数据更新没有任何问题但我真的不明白为什么! I mean for me the row component if there is no destroy triggering should be not update without sort of setter on the @Input我的意思是,如果没有销毁触发,则行组件不应该在 @Input 上没有设置器的情况下更新

Can you explain me, what I miss?你能解释一下,我想念什么吗?

the answer to your question becomes obvious when you understand what trackBy is used for (eg https://netbasal.com/angular-2-improve-performance-with-trackby-cc147b5104e5 ): using trackBy you are telling angular not to destroy and re-create ngFor iterated elements, but reuse them instead.当您了解trackBy的用途时,问题的答案就变得显而易见(例如https://netbasal.com/angular-2-improve-performance-with-trackby-cc147b5104e5 ):使用trackBy您是在告诉 angular 不要破坏和重新创建ngFor迭代元素,但重用它们。 So ngOnDestroy is not triggered because components are never destroyed, but reused with new inputs.所以ngOnDestroy不会被触发,因为组件永远不会被销毁,而是与新输入一起重用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM