Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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 在React中为div添加背景图像? render(){ const{classes}=this.props; const{imageDescription}=this.props.imgDesc; const{currentIndex}=this.state; var fullPath=`../images/Large/${(imageDescription[currentIndex]).imagePath}.jpg`; 日志(“完整路径:+Fullpath”); 返回(_Javascript_Reactjs_Material Ui - Fatal编程技术网

Javascript 在React中为div添加背景图像? render(){ const{classes}=this.props; const{imageDescription}=this.props.imgDesc; const{currentIndex}=this.state; var fullPath=`../images/Large/${(imageDescription[currentIndex]).imagePath}.jpg`; 日志(“完整路径:+Fullpath”); 返回(

Javascript 在React中为div添加背景图像? render(){ const{classes}=this.props; const{imageDescription}=this.props.imgDesc; const{currentIndex}=this.state; var fullPath=`../images/Large/${(imageDescription[currentIndex]).imagePath}.jpg`; 日志(“完整路径:+Fullpath”); 返回(,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我有一个组件返回一个带有背景图像的div,但当它启动时显示错误,找不到模块“../images/Large/homeTecno.jpg”。 但是,当我使用console.log语句的输出更改require中的变量fullPath时,它会工作?这是因为require仅用于导入javascript实体(类、对象、函数等)但不是图像。设置背景图像时,只需要字符串,而不是对象。这就是第二个解决方案有效的原因 请注意,还可以使用对象定义样式特性,特性以camelCase格式编写 考虑到您的图像可以通过yo

我有一个组件返回一个带有背景图像的div,但当它启动时显示错误,找不到模块“../images/Large/homeTecno.jpg”。
但是,当我使用console.log语句的输出更改require中的变量fullPath时,它会工作?

这是因为require仅用于导入javascript实体(类、对象、函数等)但不是图像。设置背景图像时,只需要字符串,而不是对象。这就是第二个解决方案有效的原因

请注意,还可以使用对象定义样式特性,特性以camelCase格式编写

考虑到您的图像可以通过
yourdomain.com/images/Large/{…}.jpg
访问,您应该做得很好

render(){
        const { classes } = this.props;
        const { imageDescription } = this.props.imgDesc;
        const { currentIndex } = this.state;

        var fullPath = `../images/Large/${(imageDescription[currentIndex]).imagePath}.jpg`;
        console.log("Fullpath: "+fullPath);

        return (
            <div style={{ 'background-image': "url(" + require(fullPath) + ")" }}>
            </div>
constpath=(imageDescription[currentIndex]).imagePath;
const fullPath=`url(/images/Large/${path}.jpg)`;
返回(
);
还是快一点

const path = (imageDescription[currentIndex]).imagePath;
const fullPath = `url(/images/Large/${path}.jpg)`;
return (
    <div style={{ backgroundImage: fullPath }} />
);
constpath=(imageDescription[currentIndex]).imagePath;
const backgroundImage=`url(/images/Large/${path}.jpg)`;
返回(
);

这是因为将对象写入
{object}
相当于写入
{object:object}

这是因为require仅用于导入javascript实体(类、对象、函数等)但不是图像。设置背景图像时,只需要字符串,而不是对象。这就是第二个解决方案有效的原因

请注意,还可以使用对象定义样式特性,特性以camelCase格式编写

考虑到您的图像可以通过
yourdomain.com/images/Large/{…}.jpg
访问,您应该做得很好

render(){
        const { classes } = this.props;
        const { imageDescription } = this.props.imgDesc;
        const { currentIndex } = this.state;

        var fullPath = `../images/Large/${(imageDescription[currentIndex]).imagePath}.jpg`;
        console.log("Fullpath: "+fullPath);

        return (
            <div style={{ 'background-image': "url(" + require(fullPath) + ")" }}>
            </div>
constpath=(imageDescription[currentIndex]).imagePath;
const fullPath=`url(/images/Large/${path}.jpg)`;
返回(
);
还是快一点

const path = (imageDescription[currentIndex]).imagePath;
const fullPath = `url(/images/Large/${path}.jpg)`;
return (
    <div style={{ backgroundImage: fullPath }} />
);
constpath=(imageDescription[currentIndex]).imagePath;
const backgroundImage=`url(/images/Large/${path}.jpg)`;
返回(
);

这是因为将对象写入
{object}
相当于写入
{object:object}

谢谢@Kishlin。我使用webpack创建了该项目,因此我认为我无法直接引用需要使用require语句的图像路径。webpack不处理图像。只有客户端浏览器才能根据您提供的路径获取图像。这就像h中的标记、样式表或javascript文件链接一样谢谢,下面是它的工作原理,我只是将图片文件夹从src文件夹剪切粘贴到dist文件夹(public)。您不应该这样做,因为您正在从根文件夹测试应用程序。如果移动图像,其路径将以“dist”开头,并且在开发过程中您将无法访问它们。您应该将图像文件夹保留在根目录中。在生产环境中,部署应用程序时,您将在与dist文件夹内容相同的目录。谢谢@Kishlin。我使用webpack创建了该项目,因此我认为我无法直接引用需要使用require语句的图像路径。webpack不处理图像。只有客户端浏览器才能根据您提供的路径获取图像。这就像标签或链接一样o一个html样式表或javascript文件。谢谢,下面是它的工作原理。我只是将images文件夹从src文件夹剪切并粘贴到dist文件夹(public)。您不应该这样做,因为您正在从根文件夹测试应用程序。如果移动图像,其路径将以“dist”开头,并且在开发过程中您将无法访问它们。您应该将图像文件夹保留在根目录中。在生产环境中,部署应用程序时,您将在与dist文件夹的内容相同的目录。