简体   繁体   English

图像未加载到 Angular 9

[英]Image Not Loading in Angular 9

HTML File: HTML 文件:

<div>
<img src="/src/assets/img/menu.png" alt="LOGO">
</div>

Here the New-Google-Logo.png is in the same folder as in the html file.这里的 New-Google-Logo.png 与 html 文件位于同一文件夹中。 But after ng serve the html page loads with other details, but not the image.但是在 ng 服务之后,html 页面会加载其他详细信息,但不会加载图像。 Tried by directly giving a link for an image (like www.google.com/images/x.png), it works, but local file is not loading.通过直接提供图像链接(如 www.google.com/images/x.png)尝试,它可以工作,但本地文件未加载。

Folder Tree:文件夹树:

src
  -assets
    -img
              - menu.png

Here's how it looks:这是它的外观:

Image path is correct, I have also added static assets in assets folder which angular-cli serves with default.angular-cli.json图像路径是正确的,我还在资产文件夹中添加了 static 资产,angular-cli 与 default.angular-cli.json 一起使用

"assets": [
              "src/favicon.ico",
              "src/assets"
            ]

Is there any thing i am missing?有什么我想念的吗?

Edit 2: I get this error in console GET localhost:4200/src/assets/img/menu.png 404 (Not Found)编辑 2:我在控制台 GET localhost:4200/src/assets/img/menu.png 404(未找到)中收到此错误

After several tries I got it.经过几次尝试,我得到了它。 Since the assets are configured to sit in src/assets由于资产配置为位于 src/assets

"assets": [
              "src/favicon.ico",
              "src/assets"
            ]

This is how you should add the path:这是你应该如何添加路径:

<div>
<img src="./assets/img/menu.png" alt="LOGO">
</div>

1.You can try to stop the nodejs server, then change the name of the file to a new one. 1.您可以尝试停止nodejs服务器,然后将文件名更改为新的。 2.You can double check the extension of the image if it is.png.jpeg or smth. 2.您可以仔细检查图像的扩展名是.png.jpeg还是smth。 3.I would suggest slowly and manually writing the path of the image in your IDE so it can autofill the path of the image. 3.我建议慢慢地手动在你的IDE中写入图像的路径,这样它就可以自动填充图像的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM