要解决“Angular - 异步服务器调用正在更新画布上下文,但画布视图没有更新”的问题,可以尝试以下方法:
zone.run()方法。示例代码如下:import { Component, NgZone } from '@angular/core';
@Component({
  selector: 'app-canvas',
  template: '',
})
export class CanvasComponent {
  @ViewChild('canvas') canvasRef!: ElementRef;
  
  constructor(private zone: NgZone) { }
  
  updateCanvas() {
    // 异步服务器调用
    asyncServerCall().then(() => {
      // 更新画布上下文
      const canvas = this.canvasRef.nativeElement as HTMLCanvasElement;
      const context = canvas.getContext('2d');
      // 更新画布视图
      this.zone.run(() => {
        // 更新画布上下文
        // ...
      });
    });
  }
}
changeDetectorRef.detectChanges()方法来触发变更检测,强制更新视图。示例代码如下:import { Component, ChangeDetectorRef } from '@angular/core';
@Component({
  selector: 'app-canvas',
  template: '',
})
export class CanvasComponent {
  @ViewChild('canvas') canvasRef!: ElementRef;
  
  constructor(private changeDetectorRef: ChangeDetectorRef) { }
  
  updateCanvas() {
    // 异步服务器调用
    asyncServerCall().then(() => {
      // 更新画布上下文
      const canvas = this.canvasRef.nativeElement as HTMLCanvasElement;
      const context = canvas.getContext('2d');
      // 更新画布视图
      this.changeDetectorRef.detectChanges();
    });
  }
}
请注意,以上代码示例仅为概念演示,你需要根据你的具体代码和需求进行适当的调整。