【问题标题】:Why does this syntax work over the other one with d3.json requests?为什么这种语法对 d3.json 请求的另一种有效?
【发布时间】:2018-03-28 05:58:23
【问题描述】:

我不明白为什么我发现的所有示例都适用于这样的 json 请求:

d3.json("file.json", function(data){
   ..do something with data
});

..但是当我必须实现它时,我不得不使用:

d3.json( "file.json", function(data){}).then(function(data){
    ..do something with data
)};

那里发生了什么? 我正在使用节点运行实时服务器。

【问题讨论】:

  • Pssst你用了多长时间才注意到你的回调函数没有被使用?

标签: javascript json d3.js


【解决方案1】:

很可能是因为您使用的是刚刚发布的d3 v5,而大多数遗留示例都在使用d3 v3d3 v4

Changes in D3 5.0

新的 v5 做法确实是(引自变更日志):

d3.json("file.json").then(function(data) {
  console.log(data);
});

如果之前是 (v3, v4):

d3.json("file.json", function(error, data) {
  if (error) throw error;
  console.log(data);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2016-03-30
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多