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

相关内容

热门资讯

揭露透视!aapoker俱乐部... 揭露透视!aapoker俱乐部靠谱吗,微信小程序微乐房间的挂多少钱,要领教程(有挂总结)-哔哩哔哩1...
分享透视!pokermaste... 分享透视!pokermaster辅助器,微信小程序辅助器脚本,手册教程(有挂存在)-哔哩哔哩1、玩家...
有挂透视!pokernow辅助... 有挂透视!pokernow辅助工具,微乐手游微乐辅助免费,策略教程(真的有挂)-哔哩哔哩1、首先打开...
必备透视!wepoker插件功... 必备透视!wepoker插件功能辅助器,微信小程序微乐游戏辅助,阶段教程(存在有挂)-哔哩哔哩1、玩...
解迷透视!hhpoker有辅助... 解迷透视!hhpoker有辅助吗,微乐贵阳麻将辅助,举措教程(有挂透视)-哔哩哔哩hhpoker有辅...
详细透视!wepoker有没有... 详细透视!wepoker有没有辅助,微信微乐辅助器免费,模块教程(有挂秘笈)-哔哩哔哩1、任何wep...
关于透视!智星德州可以透视吗,... 关于透视!智星德州可以透视吗,微乐广西小程序脚本,阶段教程(有挂神器)-哔哩哔哩1、每一步都需要思考...
了解透视!wepoker有透视... 了解透视!wepoker有透视吗,2025微乐小程序黑科技,学习教程(有挂实锤)-哔哩哔哩1、上手简...
详情透视!德州透视插件,微乐房... 详情透视!德州透视插件,微乐房间辅助工具在哪下载,机巧教程(有挂方式)-哔哩哔哩1、德州透视插件模拟...
必备透视!cloudpoker... 必备透视!cloudpoker怎么开挂,微信小程序游戏充值破解,办法教程(有挂助手)-哔哩哔哩1、该...