我已经像这样在项目目录中安装了d3
依赖项:
npm install d3 --save
我已经在组件
中导入了d3
模块,代码如下:
import d3 from "d3";
没有任何错误或警告提示。不幸的是,调用d3
模块中的任何方法都会失败。
例如,我尝试绘制这个线状图表,但在该模块的第一个select
方法处出现了错误。
Uncaught (in promise) TypeError: Cannot read property 'select' of undefined.
这怎么可能?在VS Code
上悬停在d3
模块的每个方法上都会显示该方法的文档。因此,我猜测该模块不能是未定义的。
<script>
import d3 from 'd3';
const data = [99, 71, 78, 25, 36, 92];
export default {
name: 'non-vue-line-chart',
template: '<div></div>',
mounted() {
const svg = d3.select(this.$el) // the error appears on the very first line of the d3-module
.append('svg')
.attr('width', 500)
.attr('height', 270)
.append('g')
.attr('transform', 'translate(0, 10)');
const x = d3.scaleLinear().range([0, 430]);
const y = d3.scaleLinear().range([210, 0]);
d3.axisLeft().scale(x);
d3.axisTop().scale(y);
x.domain(d3.extent(data, (d, i) => i));
y.domain([0, d3.max(data, d => d)]);
const createPath = d3.line()
.x((d, i) => x(i))
.y(d => y(d));
svg.append('path').attr('d', createPath(data));
},
};
</script>
可能存在某些集成不匹配的问题,我自己无法解决。请帮忙。
import * as d3 from
d3``,第二种方法被编译器立即拒绝。 然而,我仍然遇到错误。这次我收到了以下错误:Uncaught (in promise) DOMException: Failed to execute 'appendChild' on 'Node': This node type does not support this method.
您是否建议我使用更低版本的d3? - BWappsAndmorethis.$el
为 null 或未定义,你可能需要推迟到$el
解析为 Node 后再执行。 - Daniel