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)

"css-loader": "^0.19.0",在 package.json 中安装了,这是我的 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'
        }]
    }
};
4

3 回答 3

7

你是如何编译这个的?如果是 webpack,你可能需要引入 style-loader 并module.loaders在 webpack 配置的数组中包含类似这样的内容:

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

Update: With the webpack.config.js now provided, we can confirm it needed a change in the module.loaders array. OP was using a less loader and only checking for .less files, so the exact loader object should read:

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

As suggested by @Q Liu. Leaving the original bit as if someone comes here with an error importing a .css file, it's likely what they need.

于 2016-05-10T16:58:34.847 回答
1

I used the following in my webpack config and it worked:

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

于 2017-10-03T19:52:09.627 回答
-6

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

于 2016-05-10T16:34:24.207 回答