首页 > 解决方案 > Angular 7 变更检测:NgZone.onMicrotaskEmpty 的问题

问题描述

我在 Angular 应用程序中使用 chart.js 并使用来自 API 端点的数据绘制图表。

ngOnInit(): void {
this._route.params.subscribe(p => {
  let id: number = +this._route.snapshot.params["id"];
  this.busy = this._awpsService
  .getOutcomeItem("years", id).subscribe(data => {
      this.years = data;
      this.selYear = this.years[0];

      this.ngZone.onMicrotaskEmpty.first.subscribe(() => {
        $("#year").selectpicker();
        $(window).resize();
      });

      this.getProjectItems(id);

    });
});
}

上面的代码在 angular 4 上运行得很好,但是在升级到 angular 7 之后,我不得不将我的代码更改为

this.ngZone.onMicrotaskEmpty.subscribe(() => {
            $("#year").selectpicker();
            $(window).resize();
});

因为.first在角度 7 中消失了。这增加了 CPU 利用率并使应用程序崩溃。已ngZone.onMicrotaskEmpty.first在我的应用程序中广泛使用,我不知道如何正确处理更改检测。我也尝试过这种方式

this.ngZone.onMicrotaskEmpty.subscribe({
        next: () => {
          this.ngZone.run(() => {
            $('#year').selectpicker();
            $(window).resize();
          });
        }
});

但没有运气。任何人都可以帮助我吗?谢谢

标签: angulartypescriptchart.jsangular-changedetection

解决方案


推荐阅读