d3.csv在console.log中返回index.html内容 [英] d3.csv returns index.html content in console.log
问题描述
我正在将d3 v5与reactJS一起使用.我正在调用"List"类中的d3.csv,如下所示:
I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:
import React from 'react';
import * as d3 from 'd3';
class List extends React.Component{
componentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
console.log(d);
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
并且列表将在以下"App"类中导入
and List is being imported in following 'App' class
import React, { Component } from 'react';
import logo from './logo.svg';
import List from './components/list/List';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<List/>
</div>
);
}
}
export default App;
以下是"index.js"代码:
Following is the 'index.js' code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
'root'是index.html中的div ID.通过在d3.csv内的列表文件中调用console.log,可以在控制台中获取index.html文件的内容.但是我想要CSV内容.
'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.
推荐答案
我遇到了同样的问题,这就是解决问题的方法.检查数据是否与index.html
文件位于同一目录中.就我而言,我的index.html
文件位于public/
文件夹中,因此我创建了public/data/data.csv
.然后,您可以尝试d3.csv('./data/data', function(data){...})
读取数据.
I had the same issue and here's how I solved it. Check if the data is in the same directory as the index.html
file. In my case, my index.html
file is in the public/
folder, so I created public/data/data.csv
. You can then try d3.csv('./data/data', function(data){...})
to read in the data.
这篇关于d3.csv在console.log中返回index.html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!