当我在我的应用程序中添加一张图片时,它不会显示出来。

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

When I add an image to my app it doesnt show up

问题

  1. 我将图像引用为 <img src="public/assets/images/sydney.jpg" alt=""/>
  2. 我将我的 jpg 图像放在 public/assets/images 文件夹中。
  3. 然后我成功构建了 npm start,但是当我进入我的网站时,图像不在那里。

如果需要更多信息,请留下评论。这里有一些截图。

谢谢

Code

这是应用程序,我希望图像出现在悉尼FC框中。

import React from 'react';

{/* begin snippet: js hide: false console: true babel: false */}

{/* language: lang-html */}
英文:
  1. I referenced and image as &lt;img src=&quot;public/assets/images/sydney.jpg&quot; alt=&quot;&quot;/&gt;
  2. I put my jpg image in public/assets/images folder.
  3. I then build successfully with npm start, however, when I go onto my site the image isn't there.
    Please comment if you need any more information. Here are some screenshots

Thanks

Code

Here is the app, I want the image to appear in the Sydney FC Box.

import React from &#39;react&#39;;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import React from &#39;react&#39;;

export const ALeagueTeam = () =&gt; {
    return (
        &lt;div&gt;
            &lt;section id=&quot;one&quot; class=&quot;tiles&quot;&gt;
                &lt;article&gt;
									&lt;span class=&quot;image&quot;&gt;
									&lt;img src=&quot;public/assets/images/sydney.jpg&quot; alt=&quot;&quot;/&gt;
									&lt;/span&gt;
                    &lt;header class=&quot;major&quot;&gt;
                        &lt;h3&gt;&lt;a href=&quot;landing.html&quot; class=&quot;link&quot;&gt;SYDNEY FC&lt;/a&gt;&lt;/h3&gt;
                        &lt;p&gt;FOUNDED IN 2004&lt;/p&gt;
                    &lt;/header&gt;
                &lt;/article&gt;
                &lt;article&gt;
									&lt;span class=&quot;image&quot;&gt;
										&lt;img src=&quot;images/pic02.jpg&quot; alt=&quot;&quot;/&gt;
									&lt;/span&gt;
                    &lt;header class=&quot;major&quot;&gt;
                        &lt;h3&gt;&lt;a href=&quot;landing.html&quot; class=&quot;link&quot;&gt;VIEW PLAYERS&lt;/a&gt;&lt;/h3&gt;
                        &lt;p&gt;CURRENTLY 1st&lt;/p&gt;
                    &lt;/header&gt;
                &lt;/article&gt;
                &lt;article&gt;
									&lt;span class=&quot;image&quot;&gt;
										&lt;img src=&quot;images/pic03.jpg&quot; alt=&quot;&quot;/&gt;
									&lt;/span&gt;
                    &lt;header class=&quot;major&quot;&gt;
                        &lt;h3&gt;&lt;a href=&quot;landing.html&quot; class=&quot;link&quot;&gt;MELBOURNE CITY&lt;/a&gt;&lt;/h3&gt;
                        &lt;p&gt;FOUNDED IN&lt;/p&gt;
                    &lt;/header&gt;
                &lt;/article&gt;
                &lt;article&gt;
									&lt;span class=&quot;image&quot;&gt;
										&lt;img src=&quot;images/pic04.jpg&quot; alt=&quot;&quot;/&gt;
									&lt;/span&gt;
                    &lt;header class=&quot;major&quot;&gt;
                        &lt;h3&gt;&lt;a href=&quot;landing.html&quot; class=&quot;link&quot;&gt;VIEW PLAYERS&lt;/a&gt;&lt;/h3&gt;
                        &lt;p&gt;CURRENTLY 2ND&lt;/p&gt;
                    &lt;/header&gt;
                &lt;/article&gt;
                &lt;article&gt;
									&lt;span class=&quot;image&quot;&gt;
										&lt;img src=&quot;images/pic05.jpg&quot; alt=&quot;&quot;/&gt;
									&lt;/span&gt;
                    &lt;header class=&quot;major&quot;&gt;
                        &lt;h3&gt;&lt;a href=&quot;landing.html&quot; class=&quot;link&quot;&gt;WELLINGTON PHOENIX&lt;/a&gt;&lt;/h3&gt;
                        &lt;p&gt;FOUNDED IN &lt;/p&gt;
                    &lt;/header&gt;
                &lt;/article&gt;
                &lt;article&gt;
									&lt;span class=&quot;image&quot;&gt;
										&lt;img src=&quot;images/pic06.jpg&quot; alt=&quot;&quot;/&gt;
									&lt;/span&gt;
                    &lt;header class=&quot;major&quot;&gt;
                        &lt;h3&gt;&lt;a href=&quot;landing.html&quot; class=&quot;link&quot;&gt;VIEW PLAYERS&lt;/a&gt;&lt;/h3&gt;
                        &lt;p&gt;CURRENTLY 3RD&lt;/p&gt;
                    &lt;/header&gt;
                &lt;/article&gt;
            &lt;/section&gt;
        &lt;/div&gt;
    );
};

<!-- end snippet -->

答案1

得分: 1

#1 在顶部使用导入语句

import logo from './logo.png';

然后

return <img src={logo} alt="Logo" />;

#2 使用公共文件夹,并在构建过程中使用环境变量替代路径

return <img src={process.env.PUBLIC_URL + '/img/logo.png'} />;

这两种方法都在这里这里有详细说明。

英文:

If you are using Create React App build system, then you have two options.

#1 Use import statement at the top

import logo from &#39;./logo.png&#39;;

then

return &lt;img src={logo} alt=&quot;Logo&quot; /&gt;;

#2 Use public folder and use environment variable to substitute in the path during build

return &lt;img src={process.env.PUBLIC_URL + &#39;/img/logo.png&#39;} /&gt;;

Both are explained here and here

答案2

得分: 0

HTML:

&lt;img src=&quot;/images/sydney.jpg&quot; alt=&quot;&quot;/&gt;

REACT:

&lt;img src={require(&#39;../images/sydney.jpg&#39;)} /&gt;

这是HTML和React代码的对比,用于加载图像。确保替换每个中的图像名称,以使图像正常工作。此外,不要将图像放在公共目录中。这个问题也在这里有重复讨论。祝你好运!😀😀😀

英文:

Hello, that is html not react so instead of

HTML:

&lt;img src=&quot;/images/sydney.jpg&quot; alt=&quot;&quot;/&gt;

you need to use:

REACT:

&lt;img src={require(&#39;../images/sydney.jpg&#39;)} /&gt; 

replacing image-name.png with the correct image name for each of them. That way the image is able to work properly. Also don't put the img on the public Also it is a duplicate here. Good Luck 😀😀😀

huangapple
  • 本文由 发表于 2020年8月10日 11:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63333715.html
匿名

发表评论

匿名网友

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

确定