Angular的轮询trackBy不触发ngDestroy,但组件已更新。
创始人
2024-10-24 06:31:55
0

在使用trackBy进行轮询时,强制刷新组件的视图。可以通过触发CD检测 (ChangeDetectorRef.detectChanges())或者强制刷新 (this.ngZone.run(() => {}))来实现。以下是示例代码:

@Component({
  selector: 'my-component',
  template: `
    
  • {{ item }}
` }) export class MyComponent implements OnInit, OnDestroy { items: string[] = []; private subscription: Subscription; constructor(private myService: MyService, private cd: ChangeDetectorRef, private ngZone: NgZone) {} ngOnInit() { this.subscription = timer(0, 1000).subscribe(() => { this.myService.getItems().subscribe(items => { this.items = items; // 触发CD检测 this.cd.detectChanges(); // 或者强制刷新 this.ngZone.run(() => { }); }); }); } ngOnDestroy() { this.subscription.unsubscribe(); } trackByFn(index: number, item: string) { return item; } }

相关内容

热门资讯

透视科技!拱趴大菠萝万能挂图解... 透视科技!拱趴大菠萝万能挂图解,hhpoker怎么开透视(透视)分享教程(真实有挂)-哔哩哔哩1)拱...
透视透视挂!拱趴大菠萝辅助神器... 透视透视挂!拱趴大菠萝辅助神器,红龙poker有辅助吗(透视)必备教程(有挂功能)-哔哩哔哩1、完成...
透视能赢!德普之星辅助工具如何... 透视能赢!德普之星辅助工具如何设置,约局吧是否有挂(透视)必备教程(有挂教学)-哔哩哔哩1)德普之星...
刚刚!wepoker辅助器软件... 刚刚!wepoker辅助器软件下载,wepoker私人局怎么玩(透视)解密教程(有挂规律)-哔哩哔哩...
受玩家影响!佛手在线大菠萝为什... 受玩家影响!佛手在线大菠萝为什么都输,pokemmo免费脚本(透视)总结教程(有挂透明挂)-哔哩哔哩...
透视好牌!wepoker祈福有... 透视好牌!wepoker祈福有用吗,wepoker公共底牌(透视)辅助教程(确实有挂)-哔哩哔哩1、...
出现新变化!hhpoker到底... 出现新变化!hhpoker到底可以辅助码,hhpoker为什么一直输(透视)揭幕教程(真是有挂)-哔...
透视教程!德普之星透视辅助,s... 透视教程!德普之星透视辅助,sohoopoker辅助(透视)揭露教程(讲解有挂)-哔哩哔哩1)德普之...
来临!wepoker破解器有用... 您好,wepoker破解器有用吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
更值得关注的是!aapoker... 更值得关注的是!aapoker辅助器是真的吗,拱趴大菠萝自动计算机器人(透视)有挂教程(有挂神器)-...