问题
React beginner here. My app uses create-react-app, and I'm pulling in react-slick for a carousel. I'm trying to follow the directions that are provided in the setup for react-slick, but the following doesn't work for me:
@import "~slick-carousel/slick/slick.css";
@import "~slick-carousel/slick/slick-theme.css";
I get the following error:
./src/components/Homepage.scss
Module not found: Can't resolve '~slick-carousel/slick/slick.css' in '/Users/simon/Dev/frischideas/site-new/src/components'
It works if I add the css to index.html from the CDN, but I'd rather avoid that network call, as I believe it's affecting the rendering of the page on initial load.
I tried following the instructions for configuring create-react-app to use relative paths, but that wasn't working for me either. Maybe I'm completely misunderstanding, but I thought the ~ notation would allow me to import scss from a package in /node_modules.
Any suggestions/clarification would be greatly appreciated.
Update: adding the setup from my webpack.config file (most of which is the default from create-react-app).
{
test: /\.scss$/,
use: [
require.resolve('classnames-loader'),
require.resolve('style-loader'), {
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: 1,
localIdentName: "[name]__[local]___[hash:base64:5]"
},
},{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 6 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway,'
],
remove: false,
flexbox: 'no-2009',
}),
],
},
},{
loader: require.resolve('sass-loader'),
options: {
outputStyle: 'expanded'
}
}
],
}
回答1:
I was struggling with this issue and I finally find the solution:
- just go inside of your
node_modules
folder and findslick-carousel
folder - inside of that find
slick.scss
andslick-theme.scss
from slick folder and open them up - create two seperate files with names of
_slick.scss
and_slickTheme.scss
inside of your style folders of your project - copy all from
slick.scss
andslick-theme.scss
and put them in your newly created files (_slick.scss
and_slickTheme.scss
) - import them in your
app.scss
file - now if you are using webpack like me, I'm guessing that you getting can't resolve
'./ajax-loader.gif'
error and can't resolve font error after that - in fact those files is been used by
slick-carousel
css it self and for that we can just simple go inside your newly created_slickTheme.scss
and find these lines
/* Slider */
.slick-list {
.slick-loading & {
background: #fff slick-image-url("ajax-loader.gif") center center no-repeat;
}
}
/* Icons */
@if $slick-font-family == "slick" {
@font-face {
font-family: "slick";
src: slick-font-url("slick.eot");
src: slick-font-url("slick.eot?#iefix") format("embedded-opentype"), slick-font-url("slick.woff") format("woff"), slick-font-url("slick.ttf") format("truetype"), slick-font-url("slick.svg#slick") format("svg");
font-weight: normal;
font-style: normal;
}
}
- comment them out
回答2:
you might have missed this:
npm install slick-carousel
回答3:
you change to be this :
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
delete ~
回答4:
You Also need to install slick-carousel for css and font. That's the trick here. Cheers...
npm install slick-carousel --save
Then import css files in your App.js file.
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
回答5:
You need to have appropriate loaders. Use style-loader in combination with css-loader for the CSS. Use file-loader and url-loader in order to load the fonts and images linked within CSS files.
(Here are configuration settings for the file-loader)
So at the end, your webpack.config.js should contain something like this:
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react"
].map(require.resolve)
}
}
},
{
test: /\.css$/,
use: [
{loader: "style-loader"},
{loader: "css-loader"}
]
},
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192
}
}
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/'
}
}]
}
]
},
After that, you can simply import styles into your application entry point (e.g. /src/index.js):
import 'slick-carousel/slick/slick.css';
import 'slick-carousel/slick/slick-theme.css';
回答6:
It fails because you are importing css modules in your app, and styles from slick are not applied because they are getting cached. I've fixed it in my project like below:
In your style.scss files put this
:global {
@import 'node_modules/slick-carousel/slick/slick';
}
来源:https://stackoverflow.com/questions/48779212/react-slick-import-css-from-slick-carousel-fails