为什么 d3.path().moveTo() 函数未定义?

why d3.path().moveTo() function is undefined?

我生成路径的函数:

const getPath = (source, target) => {
    // console.log('path', path.moveTo(source.x, source.y));
    const path = d3.path()
      .moveTo(source.x, source.y)
      .lineTo(target.x, source.y)
      .lineTo(target.x, target.y)
      .toString();
    // console.log('path', path);
    return path;
  }

d3 导入语句:

import * as d3 from "d3";

源和目标结构:

{
  x: Number,
  y: Number
}

为什么 moveTo 未定义 我究竟做错了什么? 顺便说一句,我正在反应。

moveTo 没有 return 任何东西,所以它的 return 值为 undefined。它没有 return 路径,所以你不能在这里进行方法链接。您可以在 source code 中看到 moveTo:

moveTo: function(x, y) {
  this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
},

您可以根据此调整代码,它应该可以工作:

const getPath = (source, target) => {
    const path = d3.path();

    path.moveTo(source.x, source.y);
    path.lineTo(target.x, source.y);
    path.lineTo(target.x, target.y);

    return path.toString();
  }