【问题标题】:using material-ui with SCSS directly in React component直接在 React 组件中使用 Material-ui 和 SCSS
【发布时间】:2018-07-31 23:13:52
【问题描述】:

我有一个关于在 React 组件中使用样式的问题。我想使用 SCSS 而不是使用 withStyle 函数或将纯 CSS 文件导入 JS 文件。

例如,我想将我的 SCSS 文件导入到ButtonComponent.js,如下所示:

  1. import './buttonStyle.scss'
  2. 将我的主题配置放在名为theme.scss 的 SCSS 文件中
  3. theme.scss 导入到我的 SCSS 文件中
  4. 利用组件的className 中的SCSS 类。

我该怎么做?

【问题讨论】:

    标签: javascript css reactjs sass material-ui


    【解决方案1】:

    您将需要使用带有加载器的捆绑器,该加载器可以将 scss 编译成 css。

    您可以将 webpacksass-loader 一起使用。最小配置如下所示:

    // webpack.config.js
    
    var path = require('path');
    
    module.exports = {
        entry: './foo.js',
    
        output: {
          path: path.resolve(__dirname, 'dist'),
          filename: 'foo.bundle.js'
        },
    
        module: {
            rules: [{
                test: /\.scss$/,
                use: [{
                    loader: "style-loader" // creates style nodes from JS strings
                }, {
                    loader: "css-loader" // translates CSS into CommonJS
                }, {
                    loader: "sass-loader" // compiles Sass to CSS
                }]
            }]
        }
    };
    

    您可以阅读有关 webpack 入门的更多信息here

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 2020-11-18
      • 1970-01-01
      • 2020-11-23
      • 2020-06-24
      • 1970-01-01
      • 2018-09-25
      • 2018-11-23
      • 2016-12-26
      相关资源
      最近更新 更多