css - 即使代码正确,为什么我的图像没有加载?
问题描述
没有编译错误,我的页面加载。但是,我的图像在使用 CSS 调用时不会显示。下面的例子。通过研究,我看到了一些关于 web 包的信息,但我尝试安装文件加载器 NPM,但它没有用。谢谢!
[没有编译错误][1] ,kmkmmk
解决方案
尝试只导入它?
这是您可以用于内容数组的示例:
https://codesandbox.io/s/compassionate-ellis-qitz4?file=/src/App.js
import img from "./img/img.jpg"
//const img = require("./img/img.jpg")
const App = () => {
return (
<Img src={img} alt={alt} />
)
}
或者试试这个:
import React from "react";
import "./styles.css";
const content = [
{
BGLight: false,
lightTopLine: true,
TextPoint: true,
TitleMain: "SECURE APP",
description:
"Group 35’s project works towards solving the zero hunger owards solving the zero hunger SGGS because, our group ",
img: require("./img/img1.jpg"),
alt: "img",
TextPointDesc: true
},
{
BGLight: false,
lightTopLine: true,
TextPoint: true,
TitleMain: "SECURE APP",
description:
"Group 35’s project works towards solving the zero hunger owards solving the zero hunger SGGS because, our group ",
img: require("./img/img2.jpg"),
alt: "img",
TextPointDesc: true
},
{
BGLight: false,
lightTopLine: true,
TextPoint: true,
TitleMain: "SECURE APP",
description:
"Group 35’s project works towards solving the zero ",
img: require("./img/img3.jpg"),
alt: "img",
TextPointDesc: true
}
];
export default function App() {
return (
<div style={{ height: "100vh", width: "100vw" }}>
{content.map((c) => {
const image = c.img;
return <img style={{ height: "100vh", width: "100vw" }} src={image} />;
})}
</div>
);
}
如果您不知道确切的名称,这将起作用:
import React from "react";
import "./styles.css";
const content = [
{
BGLight: false,
lightTopLine: true,
TextPoint: true,
TitleMain: "SECURE APP",
description:
"Group 35’s project works towards solving the zero gry and missing meals. ",
alt: "img",
TextPointDesc: true
},
{
BGLight: false,
lightTopLine: true,
TextPoint: true,
TitleMain: "SECURE APP",
description:
"Group 35’s project works towards solving the zero tool, homeless people will have an increased chance in not going hungry and missing meals. ",
alt: "img",
TextPointDesc: true
},
{
BGLight: false,
lightTopLine: true,
TextPoint: true,
TitleMain: "SECURE APP",
description:
"Group 35’s project works towards solving the zero have an increased chance in not going hungry and missing meals. ",
alt: "img",
TextPointDesc: true
}
];
export default function App() {
return (
<div style={{ height: "100vh", width: "100vw" }}>
{content.map((c, cIndex) => {
const image = require(`./img/img${cIndex + 1}.jpg`);
return <img style={{ height: "100vh", width: "100vw" }} src={image} />;
})}
</div>
);
}
推荐阅读
- html - 如何使用 Bootstrap 中的可见类使文本出现在特定视口上?
- apple-push-notifications - 通过 HTTP2 使用 Spring WebClient 的 POST API
- swift - 在 Swift 中获取多维数组的元素类型
- go - Aerospike 测试环境
- angular9 - Angular 9 DragDrop - 如何停止 DropList“元素随机播放”动画
- python - 我想要一个 LeaderRank 算法的实现
- java - 如何在java中创建一个数字增加的5x5矩阵?
- firebase - 如何使用颤振从firebase实时检索数据
- html - Xpath:获取包含其他标签的标签内的文本
- angular - 为什么 Google 登录在 Angular 应用程序的 Angular Web 应用程序中不起作用?