javascript - 无法在反应应用程序中导入多个 CSS 文件
问题描述
我正在尝试导入以导入不同的 css 文件,用于反应应用程序中的不同元素。但它不是进口的。我做错什么了?
食谱.js
import React from "react";
import style from "style.modules.css";
const Recipe = ({ title, calories, image, ingredients }) => {
return (
<div className={style.recipe}>
<h1>{title}</h1>
<ol>
{ingredients.map((ingredient) => (
<li>{ingredient.text}</li>
))}
</ol>
<p>Calories={calories}</p>
<img className={style.image} src={image} alt=''></img>
</div>
);
};
export default Recipe;
样式.modules.css
.recipe{
border-radius: 10px;
box-shadow: 0px 5px 20px rgb(71, 71, 71);
margin: 20px;
display: flex;
flex-direction: column;
justify-content: space-around;
background: white;
align-items: center;
min-width: 40%;
}
.image{
border-radius: 50%;
width: 100px;
height: 100px;
}
解决方案
css 文件名很重要,它应该是 [name].module.css。
import React from "react";
import style from "./style.module.css";
有一个codeandbox供参考。
详细信息在:
https ://create-react-app.dev/docs/adding-a-css-modules-stylesheet/
推荐阅读
- python - 如何在 tkinter 中设置变量和默认值?
- c++ - 在处理递归错误时非法使用显式模板参数
- javascript - 尝试使用 AnimeJS 进行变形时,为什么我的 SVG 路径会跳跃?
- c# - 使用 SHA256CryptoServiceProvider 时出现实体验证错误
- powershell - Powershell 如果成功或错误发送电子邮件
- google-maps - Flutter + Google Maps:如何将相机动画到目标位置
- multithreading - 重温:如何将不可变参数传递给线程?(关于一生)
- python - 如何保持和运行程序始终形成最后一个值,所以当我再次打开程序时,程序从最后一个数量开始
- flutter - 使用 onGenerateRoute 的屏幕之间的条件退出转换
- python-3.x - 在虚拟环境中安装 PyObjC 时出错