我需要在React中获取一张图片。

huangapple go评论57阅读模式
英文:

I need to take an image in React

问题

如何将图像嵌入React组件?

我的图像位于srs/images中。
我尝试通过以下方式获取它:

useEffect(() => {
  const fetchData = async () => {
    try {
      const response = await axios.get("http://127.0.0.1:5000/api/user", {
        headers: {
          Authorization: `Bearer ${localStorage.getItem("access_token")}`,
        },
      });

      const {
        firstName,
        lastName,
        avatar
      } = response.data;
      setFirstName(firstName);
      setLastName(lastName);
      setAvatar(avatar);
    } catch (error) {
      console.error("Error fetching user data:", error);
    }
  };

  fetchData();
}, []);

我的头像看起来像../images/first_image.png,然后我无法在以下位置查看它:

<img src={avatar}
    alt={"Avatar"}
    style={{marginLeft: "5%", width: "4vh"}}/>

在我的PostgreSQL中,图像的路径如下:

../images/four_image.png

英文:

How can i take an images into React-component?
My image is in srs/images.
I try to take it by:

useEffect(() =&gt; {
  const fetchData = async() =&gt; {
    try {
      const response = await axios.get(&quot;http://127.0.0.1:5000/api/user&quot;, {
        headers: {
          Authorization: `Bearer ${localStorage.getItem(&quot;access_token&quot;)}`,
        },
      });

      const {
        firstName,
        lastName,
        avatar
      } = response.data;
      setFirstName(firstName);
      setLastName(lastName);
      setAvatar(avatar);
    } catch (error) {
      console.error(&quot;Error fetching user data:&quot;, error);
    }
  };

  fetchData();
}, []);

my avatar looks like ../images/first_image.png and than i can't view it in

&lt;img src={avatar}
    alt={&quot;Avatar&quot;}
    style={{marginLeft: &quot;5%&quot;, width: &quot;4vh&quot;}}/&gt;

in my postgres sql the image is like the path:
../images/four_image.png

答案1

得分: 1

<img src={require(avatar)} alt={"头像"} style={{marginLeft: "5%", width: "4vh"}}/>
英文:
&lt;img src={require(avatar)}
alt={&quot;Avatar&quot;}
style={{marginLeft: &quot;5%&quot;, width: &quot;4vh&quot;}}/&gt;

答案2

得分: -1

如果图像文件位于src/images文件夹中,我认为URL应该是images/first_image.png(需要删除开头的'..')。如果所有头像图像都存储在此文件夹中,最好只在数据库中存储文件名,并使img src为文件名+baseUrl:

<img src={baseUrl + avatarFilename}
    alt={"Avatar"}
    style={{marginLeft: "5%", width: "4vh"}}/>

其中

const baseUrl = 'images/';
英文:

If the image file is in src/images I think the url needs to be images/first_image.png (you need to ditch the '..' at the beginning. If all avatar images are stored in this folder, it might be best to store just the filename in the database and have the img src be the filename + a baseUrl:

&lt;img src={baseUrl + avatarFilename}
    alt={&quot;Avatar&quot;}
    style={{marginLeft: &quot;5%&quot;, width: &quot;4vh&quot;}}/&gt;

where

const baseUrl = &#39;images/&#39;

huangapple
  • 本文由 发表于 2023年5月25日 15:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定