如何在 create-react-app 中使用 scss 文件?

How to use scss file with create-react-app?

我正在使用 chartist.js 制作图表组件。我正在将 scss 文件导入我的组件,但 scss 样式不起作用。检查下面的代码:

chart.js:

import React, { Component } from 'react';
import ChartistGraph from "react-chartist";
import Legend from "chartist-plugin-legend";

import './piechart.scss';

let options = {
  width:400,
  height:500,
  labelInterpolationFnc: function(value) {
    return value[0]
  },
  plugins: [
        Legend()
    ]
};


class Chart extends Component {

  render(){
    return(
      <div>
          <div className="center">
          <ChartistGraph data={data} options={options} type="Pie"/>
          </div>
      </div>

    )}

}

export default Chart;

piechart.scss:

.ct-legend {
    position: relative;
    z-index: 10;

    li {
        position: relative;
        padding-left: 23px;
        margin-bottom: 3px;
    }

    li:before {
        width: 12px;
        height: 12px;
        position: absolute;
        left: 0;
        content: '';
        border: 3px solid transparent;
        border-radius: 2px;
    }

    li.inactive:before {
        background: transparent;
    }

    &.ct-legend-inside {
        position: absolute;
        top: 0;
        right: 0;
    }

    @for $i from 0 to length($ct-series-colors) {
        .ct-series-#{$i}:before {
            background-color: nth($ct-series-colors, $i + 1);
            border-color: nth($ct-series-colors, $i + 1);
        }
    }
}

请检查下面的截图,我没有像这样获得所需的传奇 -> https://codeyellowbv.github.io/chartist-plugin-legend/

sass/scss 需要预处理。您将不得不弹出您的项目并添加所需的处理,以下是一些示例:

https://medium.com/@Connorelsea/using-sass-with-create-react-app-7125d6913760

https://medium.com/front-end-hacking/how-to-add-sass-or-scss-to-create-react-app-c303dae4b5bc