如何使用 next.js 导入带有 space 的文件?
How do I import a file with a space using next.js?
我在搞next.js。我有一个 CSV 文件,其中包含一些我想在 table 中显示的数据。当我尝试导入 csv 时遇到奇怪的错误。
./static/data.csv 1:66
Module parse failed: Unexpected token (1:66)
You may need an appropriate loader to handle this file type.
字符 66 是 space </code></p>
<p>当我删除 space 时,它会在下一个 space 上出错。我不确定我应该做些什么。</p>
<p>代码:</p>
<pre><code>import Link from 'next/link';
import React from 'react';
import Head from 'next/head';
import Header from '../components/Header';
import NavBar from '../components/NavBar';
export default () => (
<div>
<title>Test</title>
<div class="title">
<p>
<img className="logo" src="/static/logo.png" />
<h1>Test</h1>
</p>
<br/>
</div>
<hr/>
</div>
)
const data = import('../static/data.csv');
在 next.config.js
文件中试试这个
module.exports = {
webpack: (config, options) => {
config.module.rules.push({
test: /\.csv$/,
loader: 'csv-loader',
options: {
dynamicTyping: true,
header: true,
skipEmptyLines: true
}
})
return config
}
}
我在搞next.js。我有一个 CSV 文件,其中包含一些我想在 table 中显示的数据。当我尝试导入 csv 时遇到奇怪的错误。
./static/data.csv 1:66
Module parse failed: Unexpected token (1:66)
You may need an appropriate loader to handle this file type.
字符 66 是 space </code></p>
<p>当我删除 space 时,它会在下一个 space 上出错。我不确定我应该做些什么。</p>
<p>代码:</p>
<pre><code>import Link from 'next/link';
import React from 'react';
import Head from 'next/head';
import Header from '../components/Header';
import NavBar from '../components/NavBar';
export default () => (
<div>
<title>Test</title>
<div class="title">
<p>
<img className="logo" src="/static/logo.png" />
<h1>Test</h1>
</p>
<br/>
</div>
<hr/>
</div>
)
const data = import('../static/data.csv');
在 next.config.js
文件中试试这个
module.exports = {
webpack: (config, options) => {
config.module.rules.push({
test: /\.csv$/,
loader: 'csv-loader',
options: {
dynamicTyping: true,
header: true,
skipEmptyLines: true
}
})
return config
}
}