Angular 6 Failing To Load Images From Assets

▼魔方 西西 提交于 2019-12-09 05:47:07

问题


For some reason I can't get my images to show in my app. This is the error, tests and class:

The error: GET http://localhost:4200/assets/image.png 404 (Not Found)

<img class="logo" src="../../assets/image.png">
<img class="logo" src="../assets/image.png">
<img class="logo" src="assets/image.png">
//None of these work

.logo {
    height: 100px;
    width: auto;
    padding: 10px;
    display: block;
    margin: 0 auto;
}

Any idea why this is not working? My image.png is in my assets folder which is located at src/assets/image.png.

Update:

So we did a test. We copied all the node modules and the project files to another pc and there the images loaded correctly. So I assume the problem lies outside the angular project it self.


回答1:


<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">



回答2:


Add assets folder to your angular.json.

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

Then refer the images like below,

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

This should work.




回答3:


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




回答4:


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




回答5:


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




回答6:


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;
}



回答7:


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.




回答8:


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">



回答9:


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




回答10:


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.




回答11:


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.


来源:https://stackoverflow.com/questions/50843655/angular-6-failing-to-load-images-from-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!