图像标签的动态路径名
我试图动态地将路径名传递给图像,但是我有一个错误。
这起作用:
return (
<div className='single-employee'>
{singleEmployee && (
<div>{avatar && <img src={require(`../../uploads/1.png`)} alt='' />}</div>
)}
</div
这不是
const path = "/uploads/1.png";
return (
<div className='single-employee'>
{singleEmployee && (
<div>{avatar && <img src={require(`../..${path}`)} alt='' />}</div>
)}
</div>
给我一个错误:
Uncaught Error: Cannot find module './../uploads/1.png'
at webpackEmptyContext (..*$:2:1)
at SingleEmployee (SingleEmployee.jsx:33:1)
at renderWithHooks (react-dom.development.js:16305:1)
谢谢。
I am trying to pass the path name to the image dynamically, But I have an error.
This works:
return (
<div className='single-employee'>
{singleEmployee && (
<div>{avatar && <img src={require(`../../uploads/1.png`)} alt='' />}</div>
)}
</div
This doesn't
const path = "/uploads/1.png";
return (
<div className='single-employee'>
{singleEmployee && (
<div>{avatar && <img src={require(`../..${path}`)} alt='' />}</div>
)}
</div>
It gives me an error:
Uncaught Error: Cannot find module './../uploads/1.png'
at webpackEmptyContext (..*$:2:1)
at SingleEmployee (SingleEmployee.jsx:33:1)
at renderWithHooks (react-dom.development.js:16305:1)
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样
Try like this