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

相关内容

热门资讯

透视总结!hh poker透视... 透视总结!hh poker透视器下载(透视)fishpoker透视,教程演示(有挂神器)-哔哩哔哩1...
透视必备!wepoker祈福有... 透视必备!wepoker祈福有用吗(透视)wpk模拟器,教程方针(有挂神器)-哔哩哔哩1)wepok...
透视有挂!wepoker如何设... 透视有挂!wepoker如何设置透视(透视)wepoker可以开透视吗,教程讲义(有挂技术)-哔哩哔...
透视总结!wepoker免费辅... 透视总结!wepoker免费辅助器(透视)德普之星辅助工具如何打开,教程要领(有挂猫腻)-哔哩哔哩小...
透视解密!wepoker如何设... 透视解密!wepoker如何设置透视(透视)佛手在线大菠萝为什么都输,教程指南书(有人有挂)-哔哩哔...
透视开挂!wepoker有透视... 透视开挂!wepoker有透视底牌吗(透视)pokemmo脚本手机版,教程法子(有挂存在)-哔哩哔哩...
透视解密!wepoker有插件... 透视解密!wepoker有插件吗(透视)hhpoker破解工具,教程法门(有挂教学)-哔哩哔哩;1、...
透视曝光!pokemmo辅助器... 透视曝光!pokemmo辅助器脚本下载(透视)wepoker私人局怎么玩,教程模板(真的有挂)-哔哩...
透视关于!wepoker底牌透... 透视关于!wepoker底牌透视(透视)wepoker手机版辅助,教程课程(的确有挂)-哔哩哔哩运w...
透视解密!we-poker辅助... 透视解密!we-poker辅助软件教程(透视)wpk模拟器是什么,教程手筋(有挂解密)-哔哩哔哩1、...