简体   繁体   English

如何使用 vue.js 和 d3.js 加载 csv 文件

[英]how to load csv file with vue.js and d3.js

I am trying to load a small csv file into Vue file.我正在尝试将一个小的 csv 文件加载到 Vue 文件中。 As I understand, there are two ways of doing it: one with fetch if the file is big and another one with import if the file size if small.据我了解,有两种方法可以做到:如果文件很大,则使用fetch如果文件很小,则使用import My csv is small and I want to use import to use the file with Vue.我的 csv 很小,我想使用import将文件与 Vue 一起使用。 The dataset is not loading, however, and I can not console log it.但是,数据集未加载,我无法控制台记录它。 It shows an error of "reference error "variable name"is not found".它显示“未找到引用错误“变量名”的错误。 Variable name being a variable name in my csv file.变量名是我的 csv 文件中的变量名。 I am very new to Vue, hope someone can help!我对Vue很陌生,希望有人能提供帮助!

This is my code:这是我的代码:

<template>
  <div id="app">
  </div>
</template>

<script>
import dataset from '../public/dataset.csv';
import * as d3 from 'd3';

let data = d3.csvParse(dataset);

export default {
  data() {
    return {
      dataset: []
    }
  },
  async mounted() {
    console.log(data)
  }
}
</script>

<style>

</style>

you can not declare data variable outside the data function.您不能在数据函数之外声明数据变量。 So bring that variable in data function, change its name and then make a function and use this line d3.csvParse(dataset);因此,将该变量引入 data 函数中,更改其名称,然后创建一个函数并使用这一行 d3.csvParse(dataset); there, assign it to the variable this.dataset.在那里,将其分配给变量 this.dataset。 Call that function in the mounted hook and then console.log this.dataset variable.在挂载的钩子中调用该函数,然后在 console.log this.dataset 变量中调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM