问题可能出现在Angular 6和D3 v5.5之间的版本兼容性上。你可以尝试以下解决方法:
npm install d3@5.5.0 --save
d3-selection
模块。你可以像这样导入模块:import { select } from 'd3-selection';
window
对象来访问D3。在某些情况下,D3可能需要在全局作用域中使用。例如:constructor() {
// 确保在window对象上访问D3
this.variable = window.d3;
}
detectChanges()
方法来触发变更检测。例如:import { ChangeDetectorRef } from '@angular/core';
constructor(private cdr: ChangeDetectorRef) {}
ngOnInit() {
// 在你的代码中尝试使用D3
this.variable = window.d3;
// 手动触发变更检测
this.cdr.detectChanges();
}
通过检查你的D3版本、导入正确的模块、使用正确的作用域访问D3对象以及手动触发变更检测,你应该能够解决“this.