Angular 6 Failing To Load Images From Assets

我的未来我决定 提交于 2019-12-03 06:36:56
<img class="logo" src="assets/image.png">

This is the right path, probably something else went wrong.

check that the image name or if its jpeg. maybe you added a new folder in the assets folder ? , like images if so the code should look like this

<img class="logo" src="assets/image/image.png">

Add assets folder to your angular.json.

"assets": [
    "src/assets"
 ],

Then refer the images like below,

<img src="/assets/image.png">

This should work.

src="assets/image.png" should work without issue. Have you restarted your build since you added in the image?

So the problem is that the angular project was lying in a location where it was causing an error. In my case it was lying in a bitbucket repository. I am not sure if that is the reason for the error though.

Try moving the whole angular project to a different location. That solved it for me :)

@Rak2018's solution is a good workaround if you would like to use that instead

Try this <img class="logo" src="./assets/image.png">

also right click your picture and check if the extension is PNG instead of png, if so write image.PNG

Try this:

<div class="logo"></div>

.logo {
    height: 100px;
    width: auto;
    padding: 10px;
    display: block;
    margin: 0 auto;
    background:url('assets/image.png') no-repeat center center;
}

it's because of you don't have permission to access file.

simple way in windows Move your project from drive C: to other drive. it's work fine.

Good luck.

The folder which contains your assets (like pictures) needs to be declared in assets array inside your angular.json

Angular only recognizes those assets which have been declared in angular.json or the assets which come from web.

Additionally, from all the three options following will work for you:-

<img class="logo" src="assets/image.png">

I had the same problem as well. and for me was to add a '/' before the assests directory:

I had same issue, this has turned out to be a case sensitive issue on the path.

In my case image folder under assets was in capital I on the image folder. /assets/Images/angularconnect-shield.png

so check, your image path and make sure it is exactly matching with.

Following other tutorials on the web, I created a src\app\assets\images folder and put my image there. However, my app already had an assets folder in src\assets. There are two fixes to this.

Either:

  1. Put my image in the existing src\assets folder and reference as <img src="src\assets\my-image.png"/> or
  2. Add src\app\assets to my angular.json assets declaration and reference as was my original intention of src\app\assets\images\my-image.png.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!