【发布时间】:2019-11-15 15:21:35
【问题描述】:
我搜索了很多类似的问题,但找不到解决方案。
我将 React.js 与我的工作项目集成。我正在使用 Webpack。
除样式外,一切运行正常。
我有Style.scss,我将此文件导入到我的反应文件中。它编译没有错误,但实际样式不适用于元素。
内联样式作品和其他正常包含的类也可以。
样式.scss
.wtf {
font-weight: bold;
font-size: 40px;
}
Test.js
import React from 'react';
import '../Styles/Style.scss';
const style = {
border: 'solid 5px green'
};
export default class Test extends React.Component {
render() {
return (
<div style={style} className='wtf text-danger'>
Am I React.Component? And I am working too?
</div>
);
};
}
根据上面的 sn-p,text-danger 应用红色,border:solid 5px green 也可以,但是,Style.scss 中指定的样式不起作用。
我检查了编译文件,似乎我的 scss 样式代码存在那里
这是浏览器中的结果
我的 webpack.config.js 文件内容如下:
const path = require( 'path' );
module.exports = {
mode: 'development',
entry: {
rooms: './react-src/rooms/rooms.js',
tasks: './react-src/tasks/tasks.js'
},
output: {
filename: '[name].js',
path: path.resolve('../htdocs/react-dist')
},
module: {
rules: [
{
test: /\.scss$/,
use: [
// style-loader
{ loader: 'style-loader' },
// css-loader
{
loader: 'css-loader',
options: {
modules: true
}
},
// sass-loader
{ loader: 'sass-loader' }
]
},
{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
}
]
}
};
知道是什么问题吗?
已解决
根据我的示例和我的 Webpack 配置,它正在编译样式以生成新的类名。
所以,上面突出显示的那个巨大的标记是我必须使用的实际类名。
它可以作为对象导入,因此我可以将类名作为属性访问。
这就是它的工作原理。
import React from 'react';
import style from '../Styles/Style.scss';
export default class Test extends React.Component {
render() {
return (
<div className={style.wtf}>
Am I React.Component? And I am working too?
</div>
);
};
}
【问题讨论】:
标签: reactjs webpack sass styles