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

相关内容

热门资讯

透视揭幕"hhpok... 透视揭幕"hhpoker一直输有挂吗"果然真的是有辅助技巧(哔哩哔哩);1、用户打开应用后不用登录就...
透视计算"wepok... 透视计算"wepoker有辅助器吗"aapoker透视方法(总是存在有辅助修改器)-哔哩哔哩1、we...
透视透视"wepok... 透视透视"wepoker分析"总是真的是有辅助插件(哔哩哔哩)1、游戏颠覆性的策略玩法,独创攻略技巧...
据报道"wepoke... 据报道"wepoker线上大神"hhpoker为什么一直输(真是是真的辅助下载)-哔哩哔哩1、hhp...
透视教你"wepok... 透视教你"wepoker透视脚本是什么"果然有辅助技巧(哔哩哔哩)wepoker透视脚本是什么脚本下...
据统计"hhpoke... 据统计"hhpoker有透视功能吗"cloudpoker外开挂(果然存在有辅助app)-哔哩哔哩1、...
透视美元局"wpk安... 透视美元局"wpk安卓下载辅助"hhpoker一直输有挂吗(都是真的有辅助工具)-哔哩哔哩1、全新机...
透视总结"werpl... 透视总结"werplan外开挂"本来真的是有辅助教程(哔哩哔哩)1、werplan外开挂免费脚本咨询...
围绕透视问题"红龙p... 围绕透视问题"红龙poker辅助"购买的wpk辅助在哪里下载(都是是真的辅助平台)-哔哩哔哩1、进入...
透视关于"wepok... 透视关于"wepoker有没有透视方法"都是真的是有辅助教程(哔哩哔哩)1、金币登录送、破产送、升级...