Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t按要求在react中加载局部img_Javascript_Reactjs - Fatal编程技术网

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和其他东西通过涉及路径的文件系统目录,使捆绑包更复杂。只是一个意见。