我正在使用react、redux和next.js创建一个项目,并希望在js中导入CSS文件。
我按照next.js/#css和next-css的说明操作,但发现CSS样式不起作用。
我的代码如下:
pages/index.js:
import React from 'react'
import "../style.css"
class Index extends React.Component {
render() {
return (
<div className="example">Hello World!</div>
);
}
}
export default Index
next.config.js:
const withCSS = require('@zeit/next-css')
module.exports = withCSS()
style.css:
.example {
font-size: 50px;
color: blue;
}
package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@zeit/next-css": "^0.1.5",
"next": "^6.0.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^4.0.0",
"redux-devtools": "^3.4.1"
},
"scripts": {
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"dev": "next",
"build": "next build",
"start": "next start"
}
}
问题:
1. 在Chrome中出现了“未捕获的SyntaxError”,但似乎不影响页面的渲染。但我仍然想知道原因和解决方案。index.js在Chrome中的错误如下图所示:
2. 如Chrome所示,没有“example”类,这意味着style.css文件未加载。我是否遗漏了什么?在chrome中没有CSS文件。
提前致谢。
pages
目录中创建了_document.js
文件吗?如果有的话,你能把代码粘贴在这里吗? - mtl