javascript - 反应:加载本地图像时出现错误 404
问题描述
我尝试使用图像和其他属性渲染传单列表。但是,每次我尝试使用正确的路径加载图像时,我似乎都会收到 404 错误。
错误说GET http://localhost:3000/assets/flyer1.png 404 (Not Found)
。
我很困惑,似乎也需要我启动后端来检索图像,但在这种情况下它没有意义。
我的代码:
const myClass = () => {
const events = [
{
"src": "../../assets/flyer1.png",
"eventName": "Bruin Sunday",
"eventTime": "October 11, 2020 3-4pm",
"location": "TBD"
},
{
"src": "../../assets/flyer2.png",
"eventName": "Vision 2020",
"eventTime": "October 17 12pm - 18 4pm, 2020",
"location": "TBD"
}
]
/* Renders the list of flyers from json file */
const flyerList = events.map((event) => {
return(
<Flyer src={event.src} alt={event.eventName} time={event.eventTime} location={event.location} />
)
})
return (
{flyerList}
);
}
Flyer 的定义如下:
const Flyer = (props) => {
return(
<div className="flyer-container"
onMouseEnter={()=>{setHovering(true);}}
onMouseLeave={()=>{setHovering(false);}}>
<img className="flyer-img" src={props.src} alt={props.alt}></img>
<div className="flyer-info">
<p>Event name: {props.eventName}</p>
<p>Event time: {props.eventTime}</p>
<p>Location: {props.location}</p>
</div>
</div>
);
}
任何见解都有帮助!谢谢
解决方案
我在您的代码中找不到任何错误我认为只是尝试将“src”变量的相对路径更改为绝对路径它可能会开始工作,因为某些时候路径中似乎存在冲突,例如浏览器无法找到文件的确切位置。希望它可以开始工作。
推荐阅读
- javascript - 如何在javascript中将日期时间转换为数字
- java - 如何取消预定的定时器任务并再次运行?
- opengl - 如何在 glsl 片段着色器中正确设置像素颜色?
- javascript - 未找到 React 模块,模块已安装,但运行时却找不到
- azure - 如何向使用 Azure 服务总线 Java SDK 的发布者/订阅者客户端添加重试选项(重试策略)?
- mysql - Powershell String 保持 CR & LF
- c# - TPL Dataflow 管道中的图像刷新问题
- kubernetes - AKS 管道中的 Kudu 服务
- python - 为什么按钮移动不正确
- ansible - 如何使用 Ansible 停止 VM 并验证 VM 是否已停止?