java怎么嵌套css_嵌套类在使用webpack的CSS模块中不起作用

我使用webpack和css-loader以及style-loader在我的React应用程序中启用css模块 . 这些是以下设置:

Webpack配置:

module.exports = {

mode: "development",

entry: __dirname + "/app/index.js",

module: {

rules: [

{

test: /\.js$/,

exclude: /node_modules/,

loader: "babel-loader"

},

{

test: /\.css$/,

use: [

"style-loader",

{

loader: "css-loader",

options: {

modules: true,

localIdentName: "[name]__[local]___[hash:base64:5]"

}

}

]

}

]

},

output: {

filename: "bundle.js",

path: __dirname + "/build"

},

plugins: [HTMLWebpackPluginConfig]

};

在我的React组件中,我编写了这个代码:

import React from "react";

import styles from "./Carousel.css";

class Carousel extends React.Component {

render() {

return (

className={styles["test"]}

src="https://i2.wp.com/beebom.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg?resize=640%2C426"

/>

);

}

}

export default Carousel;

在我的Carousel.css文件中:

.carousel {

background-color: red;

.test {

width: 200px;

}

}

当我检查渲染的HTML时,我可以看到carousel类及其属性来自父div . 但是,子img标签显示了类名,但没有与之关联的属性 .

知道我在这里做错了吗?

EDIT:: Sam's suggestions worked and Im summarising the changes that solved it:

由于嵌套是css的一个特性,我们需要使用sass或更少 . 为此,我使用了postcss-loader .

更新了webpack配置规则部分:

rules: [

{

test: /\.js$/,

exclude: /node_modules/,

loader: "babel-loader"

},

{

test: /\.css$/,

use: [

"style-loader",

{

loader: "css-loader",

options: {

modules: true,

localIdentName: "[name]__[local]___[hash:base64:5]"

}

},

*"postcss-loader"*

]

}

还添加了一个postcss.config.js文件,如下所示:

module.exports = {

plugins: [

require("postcss-nested")({

/* ...options */

})

]

};

并使用npm install -D选项添加了postcss-loader,postcss-nested包 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值