如何在组件的.jsx文件中导入CSS文件

3

我正在尝试使用react-day-pickers组件,但我无法弄清楚如何导入.css文件,并且我一直收到错误:

Module parse failed: 

/Users/qliu/Documents/workspace/AppNexus/pricing_ui/contract-ui/app_contract-ui/node_modules/react-day-picker/lib/style.css Unexpected token (3:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (3:0)

我在我的package.json中安装了"css-loader": "^0.19.0",,这是我的Calender.jsx文件:

import React from "react";
import DayPicker, { DateUtils } from "react-day-picker";

import "../../../node_modules/react-day-picker/lib/style.css"; // <==== ERROR ON THIS LINE

export default class Calender extends React.Component {

  state = {
    selectedDay: null
  };

  handleDayClick(e, day, modifiers) {
    if (modifiers.indexOf("disabled") > -1) {
      console.log("User clicked a disabled day.");
      return;
    }
    this.setState({
      selectedDay: day
    });
  }

  render() {

    // Add the `selected` modifier to the cell of the clicked day
    const modifiers = {
      disabled: DateUtils.isPastDay,
      selected: day => DateUtils.isSameDay(this.state.selectedDay, day)
    };

    return <DayPicker enableOutsideDays modifiers={ modifiers } onDayClick={ this.handleDayClick.bind(this) } />;
  }
}

这是我的webpack.config.js配置文件:

var path = require('path');
var webpack = require('webpack');
var settings = require('./src/server/config/settings');
var LessPluginAutoPrefix = require('less-plugin-autoprefix');

module.exports = {
    devtool: '#source-map',
    resolve: {
        extensions: ['', '.jsx', '.js']
    },
    entry: [
        'webpack-hot-middleware/client',
        './src/client/index.jsx'
    ],
    externals: {
        'react': 'React',
        'react-dom': 'ReactDOM',

        // to avoid duplicate import of React with react-addons-css-transition-group
        './React': 'React',
        './ReactDOM': 'ReactDOM',
        config: 'config'
    },
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js',
        publicPath: '/'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
    ],
    lessLoader: {
        lessPlugins: [
            new LessPluginAutoPrefix({ browsers: ['last 2 versions'] })
        ]
    },
    module: {
        loaders: [{
            test: /\.(js|jsx)$/,
            loaders: ['babel'],
            exclude: /node_modules/
        },
        {
            test: /\.less$/,
            loader: 'style!css!less'
        },
        {
            test: /\.json$/,
            loader: 'json-loader'
        }]
    }
};

1
展示你的Webpack配置。 - QoP
3个回答

7

你是如何进行编译的?如果使用的是webpack,你可能需要引入style-loader并在webpack配置文件的module.loaders数组中包含类似以下的内容:

{
  test: /\.css$/,
  loader: "style!css"
}

更新:现在提供了webpack.config.js文件,我们可以确认它需要更改module.loaders数组。OP使用的是一个less loader,并且只检查.less文件,所以精确的loader对象应该如下:

{
  test: /\.(less|css)$/,
  loader: 'style!css!less'
}

@Q Liu所建议的那样,保留原始代码。如果有人在导入.css文件时出现错误,这可能是他们需要的。


1
我在我的Webpack配置中使用了以下内容,它起作用了:

test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: true,
            },
          },
        ],


-6

JSX不是CSS。在你的主HTML文件中,只需添加一个<link>元素到CSS文件中,React生成的DOM将使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接