带有Vue的d3-此.ownerDocument未定义

cgfeq70w  于 2023-03-03  发布在  Vue.js
关注(0)|答案(1)|浏览(159)

我正在尝试将d3图表与Vue集成。使用ref似乎会出错,但这是正确的方法吗?

<template>
      <div ref="chart"></div>
</template>

const chart = ref(null);

onMounted(() => {
  
  const chartContainer = d3.select(chart);

  const margin = { top: 20, right: 20, bottom: 30, left: 50 };
  const width = chartContainer.offsetWidth - margin.left - margin.right;
  const height = chartContainer.offsetHeight - margin.top - margin.bottom;

  const svg = chartContainer
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform",`translate(${margin.left},${margin.top})`);
});

我收到此错误:

Uncaught (in promise) TypeError: this.ownerDocument is undefined
eh57zj3b

eh57zj3b1#

尝试d3.select全部,而不是d3.select

如果这不起作用,更多的提示在这里:
https://stackoverflow.com/a/49696538/7549483

相关问题