Here is a component importing css...
这是导入css的组件...
import React from 'react';
import styles from './sample.css';
class Index extends React.Component {
render(){
return (
<div>
<div className="${styles.bodywrap}"></div>
</div>
);
}
}
export default Index;
My stylesheet sample.css
is the following...
我的样式表sample.css如下......
.bodywrap {
color:red;
}
But when I run the web app I get the following error...
但是,当我运行Web应用程序时,我收到以下错误...
SyntaxError: C:/Users/Eric/Desktop/tutorHub/components/index_components/sample.c
ss: Unexpected token (1:0)
> 1 | .bodywrap {
| ^
2 | color:red;
3 | }
I followed the instuctions to setup css modules, so my webpack.config looks as follows...
我按照设置来设置css模块,所以我的webpack.config看起来如下......
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: "./main.js",
output: {
path: '/',
filename: 'index.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/,
query:{
presets: ['es2015','react'],
}
},
{
test: /\.css/,
loader: ExtractTextPlugin.extract('css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]')
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
new webpack.optimize.OccurrenceOrderPlugin()
]
};
Any idea why my CSS is not being read properly? Do I need something to convert the code so it is recognized properly?
知道为什么我的CSS没有被正确读取?我是否需要一些东西来转换代码以便正确识别?
EDIT
I think its important to node that I'm rendering the component above through the server using node js...
我认为通过使用节点js在服务器上渲染组件的节点很重要...
var express = require('express');
var router = express.Router();
var React = require('react');
var reactDom = require('react-dom/server');
var App = React.createFactory(require('../components/index'));
var MasterLayout = React.createFactory(require('../master/links'));
router.get('/', function(req,res) {
var reactHtml = reactDom.renderToString(App({}));
var styles = reactDom.renderToString(MasterLayout({}));
res.render('../../tutorHub/index.jade', {reactOutput: reactHtml, links: styles});
});
Can this be interfering with the css?
这会干扰css吗?
1 个解决方案
#1
1
In your webpack.config.js add this loader :-
在你的webpack.config.js中添加这个加载器: -
{
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
Add in your component add this :-
添加您的组件添加: -
import 'yourPath.css';
for use classes of this css file just write
对于这个css文件的使用类只是写
<div className="yourClassName" />
#1
1
In your webpack.config.js add this loader :-
在你的webpack.config.js中添加这个加载器: -
{
loaders: [
{ test: /\.css$/, loader: "style-loader!css-loader" }
]
}
Add in your component add this :-
添加您的组件添加: -
import 'yourPath.css';
for use classes of this css file just write
对于这个css文件的使用类只是写
<div className="yourClassName" />