无法使用 d3 读取项目文件夹结构中的 CSV 文件

问题描述 投票:0回答:1

我对 React 有点陌生。 问题:我有一个文件夹结构 -

  • 我的文件夹名称
    • Utils.ts
    • test_csv_file.csv

我尝试在我的

utils.ts
中读取此 CSV 文件,如下所示: `

import * as d3 from 'd3';

const getCSVData = () => {
 d3.csv('./test_csv_file.csv', function (data) {
    console.log(data);
  });
}

它抛出错误:

GET http://localhost:3000/test_csv_file.csv net::ERR_ABORTED 404 (Not Found)

我检查了几个例子,但找不到它,并且花了几乎一天的时间来解决这个问题: ReactJS + D3:解析本地 CSV 文件并将其传递给 d3-request 状态

reactjs typescript d3.js
1个回答
1
投票

您确定已将 csv 正确放置在项目中吗?

按照这个方法尝试一下,对我来说效果很好。

import * as d3 from 'd3';
import csv from "./test_csv_file.csv"

const getCSVData = () => {
    d3.csv(csv).then((data) => {
        console.log(data);
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.