簡體   English   中英

如何將css文件導入Component .jsx文件

[英]How to import css file for into Component .jsx file

我試圖使用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'
        }]
    }
};

你怎么編譯這個? 如果是webpack,你可能需要引入樣式加載器並在webpack配置的module.loaders數組中包含這樣的東西:

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

更新 :現在提供了webpack.config.js,我們可以確認它需要在module.loaders數組中進行更改。 OP使用較少的加載器並且僅檢查.less文件,因此確切的加載器對象應該是:

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

正如@Q劉建議。 保留原始位,好像有人來​​這里導入.css文件時出錯,很可能是他們需要的。

我在我的webpack配置中使用了以下內容並且它有效:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM