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脚...
第五分钟绝活!wepoker辅... 第五分钟绝活!wepoker辅助器软件下载(透视)其实是有辅助安装(哔哩哔哩)1、下载好wepoke...
第四分钟阶段!德州透视hhpo... 第四分钟阶段!德州透视hhpoker(透视)真是真的是有辅助下载(哔哩哔哩);1、下载好德州透视hh...
六分钟机巧!hhpoker是真... 六分钟机巧!hhpoker是真的还是假的(透视)竟然是真的有辅助透视(哔哩哔哩)六分钟机巧!hhpo...
第5分钟攻略!wepoker亲... 第5分钟攻略!wepoker亲友圈有用吗(透视)都是是有辅助神器(哔哩哔哩)第5分钟攻略!wepok...
七分钟方式!impoker辅助... 七分钟方式!impoker辅助(透视)竟然是真的有辅助辅助(哔哩哔哩)1.impoker辅助 选牌创...
第6分钟办法!we poker... 第6分钟办法!we poker插件(透视)确实是真的有辅助神器(哔哩哔哩)we poker插件脚本下...
第一分钟办法!wpk控制牌是真... 第一分钟办法!wpk控制牌是真的吗(透视)本来真的有辅助教程(哔哩哔哩)1、进入游戏-大厅左侧-新手...
三分钟方式!wpk辅助插件(透... 您好,wpk辅助插件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
八分钟机巧!hhpoker哪个... 八分钟机巧!hhpoker哪个俱乐部靠谱(透视)好像有辅助透视(哔哩哔哩)1、hhpoker哪个俱乐...