Javascript Can';t按要求在react中加载局部img
我想在贴图循环中渲染局部图像 我在许多帖子中看到我应该使用Javascript Can';t按要求在react中加载局部img,javascript,reactjs,Javascript,Reactjs,我想在贴图循环中渲染局部图像 我在许多帖子中看到我应该使用require const urlBase = './../../assets/'; const CompList = (props) => { return props.skills.map((elm, index) => { console.log(`${urlBase}${elm.imageName}`) return <Chip key={`comp
require
const urlBase = './../../assets/';
const CompList = (props) => {
return props.skills.map((elm, index) => {
console.log(`${urlBase}${elm.imageName}`)
return <Chip
key={`comp-${index}`}
avatar={<Avatar alt="Angular" src={require(`${urlBase}${elm.imageName}`)} />}
label={elm.techno}
/>
})
}
const urlBase='./../../../assets/';
常量列表=(道具)=>{
返回道具.技能.地图((elm,索引)=>{
console.log(`${urlBase}${elm.imageName}`)
返回
})
}
但我得到了这个错误
找不到模块“/../../assets/Angular.png”
该应用程序是使用
create react app
生成的。您可以这样做-
const imageToShow = require('../path-to-the-file.extension');
<Chip
key={`comp-${index}`}
avatar={<Avatar alt="Angular" src={imageToShow} />}
label={elm.techno}
/>
const imageToShow=require('../path to the file.extension');
这应该可以,请重新创建错误。区别是什么?项目的相对路径工作得更好,在他的例子中,urlBase和其他东西通过涉及路径的文件系统目录,使捆绑包更复杂。只是一个意见。