简体   繁体   English

Angular 6 无法从资产加载图像

[英]Angular 6 Failing To Load Images From Assets

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.我的 image.png 位于我的资产文件夹中,该文件夹位于 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.所以我认为问题出在它本身的角度项目之外。

<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.检查图像名称或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 文件夹添加到 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. src="assets/image.png"应该可以正常工作。 Have you restarted your build since you added in the image?自从添加图像以来,您是否重新启动了构建?

The folder which contains your assets (like pictures) needs to be declared in assets array inside your angular.json包含您的资产(如图片)的文件夹需要在 angular.json 内的资产数组中声明

Angular only recognizes those assets which have been declared in angular.json or the assets which come from web. Angular 只识别那些在 angular.json 中声明的资产或来自 web 的资产。

Additionally, from all the three options following will work for you:-此外,以下所有三个选项都适合您:-

<img class="logo" src="assets/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;
}

So the problem is that the angular project was lying in a location where it was causing an error.所以问题是 angular 项目位于导致错误的位置。 In my case it was lying in a bitbucket repository.就我而言,它位于一个 bitbucket 存储库中。 I am not sure if that is the reason for the error though.我不确定这是否是错误的原因。

Try moving the whole angular project to a different location.尝试将整个 angular 项目移动到不同的位置。 That solved it for me :)那为我解决了:)

@Rak2018's solution is a good workaround if you would like to use that instead如果您想改用 @Rak2018 的解决方案是一个很好的解决方法

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. Windows 中的简单方法将您的项目从驱动器 C: 移动到其他驱动器。 it's work fine.它工作正常。

Good luck.祝你好运。

Try this <img class="logo" src="./assets/image.png">试试这个<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也右键单击您的图片并检查扩展名是否为PNG而不是png,如果是,请写image.PNG

Following other tutorials on the web, I created a src\\app\\assets\\images folder and put my image there.按照网络上的其他教程,我创建了一个src\\app\\assets\\images文件夹并将我的图像放在那里。 However, my app already had an assets folder in src\\assets .但是,我的应用程序在src\\assets已经有一个 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将我的图像放在现有的src\\assets文件夹中并引用为<img src="src\\assets\\my-image.png"/>
  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 .src\\app\\assets添加到我的 angular.json 资产声明和引用中,这是我对src\\app\\assets\\images\\my-image.png

I had the same problem as well.我也有同样的问题。 and for me was to add a '/' before the assests directory:对我来说是在assests目录之前添加一个“/”:

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.在我的情况下,资产下的图像文件夹在图像文件夹中的大写字母 I 中。 /assets/Images/angularconnect-shield.png /assets/Images/angularconnect-shield.png

so check, your image path and make sure it is exactly matching with.所以检查,你的图像路径并确保它完全匹配。

If it's due to case-sensitivity in URLs hosted by 'ng serve', there is an option to disable that.如果是由于 'ng serve' 托管的 URL 区分大小写,则可以选择禁用它。 'ng serve' uses webpack-dev-server which uses http-proxy-middleware. 'ng serve' 使用 webpack-dev-server,它使用 http-proxy-middleware。 You can write a custom middleware script which corrects the url-case to match the file-case (it only affects the server response, the browser still sees the original url).您可以编写一个自定义中间件脚本来更正 url-case 以匹配文件大小写(它只影响服务器响应,浏览器仍会看到原始 url)。

An example of how to code that is posted here: https://github.com/webpack/webpack-dev-server/issues/578#issuecomment-569121500此处发布的如何编码的示例: https : //github.com/webpack/webpack-dev-server/issues/578#issuecomment-569121500

It could be due to parentheses in your workspace path if you're using an old version of Angular: https://stackoverflow.com/a/56736932/4179032 .如果您使用的是旧版本的 Angular,这可能是由于工作区路径中的括号所致: https : //stackoverflow.com/a/56736932/4179032 (I tried it in Angular 12.2.8 and it's fixed in that at least.) (我在 Angular 12.2.8 中尝试过,至少它已修复。)

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

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