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

相关内容

热门资讯

透视阶段!悦扑克脚本,poke... 透视阶段!悦扑克脚本,pokemmo脚本手机版(透视)总是有脚本工具(哔哩哔哩)1、pokemmo脚...
透视步骤!wepoker辅助器... 透视步骤!wepoker辅助器软件下载,购买的wpk辅助在哪里下载(透视)其实存在有挂(哔哩哔哩)1...
透视步骤!聚星ai辅助工具激活... 透视步骤!聚星ai辅助工具激活码,poker辅助器免费安装(透视)一直是真的挂(哔哩哔哩)1、透视步...
透视经验!wpk有辅助器吗,w... 透视经验!wpk有辅助器吗,wepoker免费脚本(透视)总是是真的挂(哔哩哔哩)一、wepoker...
透视绝活儿!wpk辅助是什么,... 透视绝活儿!wpk辅助是什么,wpk透视插件(透视)一直是真的脚本脚本(哔哩哔哩)1)wpk辅助是什...
透视积累!wepoker高级辅... 透视积累!wepoker高级辅助,大菠萝手游辅助(透视)切实真的有挂(哔哩哔哩)1、每一步都需要思考...
透视烘培!wepokerplu... 透视烘培!wepokerplus透视脚本免费,德州圈脚本(透视)真是存在有挂(哔哩哔哩)透视烘培!w...
透视阶段!wepokerh5破... 透视阶段!wepokerh5破解,wepoker永久免费脚本(透视)总是真的是有挂(哔哩哔哩)1、任...
透视课程!wepoker钻石怎... 透视课程!wepoker钻石怎么看底牌,wepoker有透视吗(透视)一贯真的是有透视神器(哔哩哔哩...
透视总结!pokemmo辅助官... 透视总结!pokemmo辅助官网,wepoker私人局可以透视(透视)本来是有挂(哔哩哔哩)1、we...