如何将CSS文件导入到Component .jsx文件中 [英] How to import css file for into Component .jsx file

查看:174
本文介绍了如何将CSS文件导入到Component .jsx文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像这样使用react-day-pickers组件,但是我不知道如何导入.css文件,并且不断收到错误消息:

I am trying to use the react-day-pickers component like so but I can't figure out how to import the .css file and I keep getting the error:

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文件:

I have "css-loader": "^0.19.0", in my package.json installed and here is my Calender.jsx file:

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:

and this is my 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'
        }]
    }
};

推荐答案

您如何编译它?如果是webpack,则可能需要引入样式加载器,并在webpack配置的module.loaders数组中包含以下内容:

How are you compiling this? If it's webpack, you'd probably need to bring in the style-loader and include something like this in the module.loaders array in your webpack config:

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


更新:使用现在提供的webpack.config.js,我们可以确认需要在module.loaders数组中进行更改. OP使用较少的加载程序,仅检查.less文件,因此确切的加载程序对象应为:


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'
}

根据 @Q Liu 的建议.好像有人来​​导入.css文件时出错一样,这很可能就是他们所需要的.

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.

这篇关于如何将CSS文件导入到Component .jsx文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆