gpt4 book ai didi

css - 如何将 css 文件导入组件 .jsx 文件

转载 作者:行者123 更新时间:2023-11-28 10:13:27 25 4
gpt4 key购买 nike

我正在尝试像这样使用 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 loader 并且只检查 .less 文件,所以确切的 loader 对象应该是:

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

根据 @Q Liu 的建议.保留原始位,就好像有人带着导入 .css 文件的错误来到这里,这很可能是他们所需要的。

关于css - 如何将 css 文件导入组件 .jsx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37143885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com