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; } }

相关内容

热门资讯

透视有挂!pokemmo辅助工... 透视有挂!pokemmo辅助工具(透视)原来存在有辅助插件(真的有挂)-哔哩哔哩1.pokemmo辅...
透视肯定!智星德州插件2024... 透视肯定!智星德州插件2024最新版(透视)切实有辅助插件(有挂功能)-哔哩哔哩智星德州插件2024...
有消息称!we-poker是什... 有消息称!we-poker是什么软件(透视)本来真的有辅助软件(有挂教程)-哔哩哔哩1、打开软件启动...
据通报!佛手在线大菠萝智能辅助... 据通报!佛手在线大菠萝智能辅助器(透视)确实是真的辅助安装(有挂总结)-哔哩哔哩1、操作简单,无需佛...
透视规律!wepoker辅助器... 透视规律!wepoker辅助器(透视)果然存在有辅助神器(有挂细节)-哔哩哔哩1、操作简单,无需we...
无独有偶!poker worl... 无独有偶!poker world辅助器(透视)竟然有辅助app(有挂实锤)-哔哩哔哩1、poker ...
透视透视!htx矩阵wepok... 透视透视!htx矩阵wepoker辅助(透视)果然存在有辅助平台(有挂细节)-哔哩哔哩1、进入游戏-...
截至发稿!wejoker辅助脚... 截至发稿!wejoker辅助脚本(透视)确实存在有辅助app(真的有挂)-哔哩哔哩1)wejoker...
透视中牌率!wepoker透视... 透视中牌率!wepoker透视脚本免费使用视频(透视)确实存在有辅助工具(有挂功能)-哔哩哔哩1、很...
透视免费!wepoker安装教... 您好,wepoker安装教程这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...