gpt4 book ai didi

reactjs - 如何在ReactJS中制作动态图像?

转载 作者:行者123 更新时间:2023-12-03 14:17:58 25 4
gpt4 key购买 nike

在我的系统中,我有一些用户可以呈现的图像,这对我来说非常有利,能够只传递一个 ID,然后将该图像呈现给用户。

(一个用例:系统中的文章有与之关联的图像,系统中有很多文章,最好只提取图像 ID 并根据该 ID 动态显示图像。我没有现实可行的方法可以静态地为每篇文章提供图像路径。)

不幸的是,我已经尝试解决这个问题,但还没有取得进展。我非常感谢“像我 5 岁一样向我解释一下”这个问题的答案。

我一直在尝试以两种不同的方式使用图像,但不幸的是,两种方式都对我不起作用。不幸的是我需要两者才能继续:(

1:将其作为div的背景。 [我将其用作此处覆盖文本的轮播。]

2:将其作为独立图像。 [这将弥补通过网络应用程序使用图像的 95% 以上的情况。]

我希望在一个完美的世界中将这样的对象传递给 props。

  articleDetails = {
articleId: 38387,
articleTitle: "Magical Unicorn Article"
articleContent: "I am not that creative, but here is some content."
}


<IndividualArticle article={articleDetails}/>

然后让 props 将其接收并将其转换为我本地文件的图像路径。

   templateStringForImage = `../../../../articleImages/${this.props.article.articleId}.png`

然后将此模板字符串用作:

1:div的背景图片

    <div
className="container"
style={{
backgroundImage: `url(${templateStringForImage})` ,
height: "576px"
}}
enter code here

</div>

2:独立图像

      <Image
src={require({templateStringForImage})}
/>

我尝试过的一些事情:

我尝试了一些更改,例如将代码更改为:

     var Background = `../../../images/articleImages/${         
this.props.article.image
}`;

     <div style={{                
backgroundImage: url('../../../images/articleImages/article3Image.png'),
height: "576px"
}}

但不幸的是,这只给了我这些错误。

 Line 32:  'url' is not defined  no-undef

此外,当我尝试像这样使用新的背景对象时,我遇到了不同的错误

             var Background = `../../../images/articleImages/${         
this.props.article.image
}`;

<Image
src={require({Background})}
/>

错误是:

         Error: Cannot find module '[object Object]'

我当前的系统/错误:

-我在图像文件夹的 src 代码中指定了一堆图像,例如article1Image.png、article2Image.png、article3Image.png。

-我想将“article1Image.png”直接传递到对象中并生成图像。

-我尝试做这样的事情,但它一直失败,不幸的是,当我尝试将它用作 div 上的背景图像时,我什至没有收到错误消息......它实际上只是屏幕上的空白区域。没有损坏的图像图标,只是一片空白。

   var Background = `'../../../images/articleImages/${
this.props.article.image
}'`;

<div
style={{
backgroundImage: `url(${Background})`,
height: "576px"
}}

-当尝试使用语义 UI 的图像时,我也遇到了一个令人讨厌的错误

      <Image
src={require(Background)}
/>

错误:

 Error: Cannot find module ''../../../images/articleImages/article2Image.png''

然而,令人震惊的是,当我将它作为这样的静态字符串提供时,它会起作用。

      <Image
src={require('../../../images/articleImages/article2Image.png')}
/>

但是即使我将字符串路径直接输入到 div 的背景图像中,它仍然显示为空...

      <div
style={{
backgroundImage: `url(../../../images/articleImages/article3Image.png)`,
height: "576px"
}}

谢谢大家,我非常感谢大家的帮助!

最佳答案

根据您的尝试,有 2 种不同的解决方案。

网址

如果您想通过 style={{ backgroundImage: 'url(...)' }} 访问它们,则需要可以从前端访问该 url。如果您使用 create-react-app,这意味着将图像放置在 public 文件夹中,而不是 src 文件夹中。

因此,如果公共(public)文件夹中有图像,例如:/public/articleImages/article2Image.png,则可以使用 url 样式属性:

const imageName = 'article2Image.png';
<div style={{ backgroundImage: `url('/articleImages/${imageName}')` }}>
...
</div>
<img src={`/articleImages/${imageName}`} />

如果您没有使用 create-react-app 配置,那么您需要通过为应用程序提供服务的方式(即 express)来提供图像。

要求

这个有点棘手,我也不是 100% 知道它不能开箱即用的原因。

测试时我必须做的是要求文件顶部的所有图像进行硬编码,然后我就可以使用 require(stringTemplate) 了。但我收到了错误:fileName尚未转译。,所以我不确定相同的修复是否适合您。

它基本上看起来像这样:

require('../../../images/articleImages/article1Image.png');
require('../../../images/articleImages/article2Image.png');
...
// This code would be inside a component
const imageName = 'article2Image.png';
const filePath = '../../../images/articleImages/${imageName}';
const fileUrl = require(filePath);
<div style={{ backgroundImage: `url(${fileUrl})` }}>
...
</div>
<img src={fileUrl} />

如果文件顶部没有 require,编译就会出现问题。

<小时/>

我的建议是使用静态资源路径,这样您就可以使用上面的 URL 技术。 require 更适合像图标这样不经常更改的静态文件。

请随意提出任何问题,我很乐意澄清并提供更多示例。

关于reactjs - 如何在ReactJS中制作动态图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com