简体   繁体   English

图像未在 Angular 2 中加载

[英]Images not loading in Angular 2

HTML File : HTML 文件:

 <div>
      <img  src="New-Google-Logo.png"/>
    </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
  -app
    -logincomponent
              - logincomponent.html
              - logincomponent.css
              - New-Google-Logo.png
              - logincomponent.ts
     -homecomponent
              - homecomponent.html
              - homecomponent.css
              - homecomponent.ts

Here the New-Google.png is referred inside logincomponent.html as given above.这里 New-Google.png 是在上面给出的 logincomponent.html 中引用的。

Try 2 :尝试 2:

src
  -app
    -logincomponent
              - logincomponent.html
              - logincomponent.css
              - Images
                - New-Google-Logo.png
              - logincomponent.ts

And referred in the html like :并在 html 中引用,如:

<div>
      <img  src="./images/New-Google-Logo.png"/>
 </div>

Both these didn't worked out.这两个都没有解决。

If you are using angular-cli then all your static assets should be kept in assets folder.如果您使用 angular-cli,那么您的所有静态资产都应保存在资产文件夹中。 Then you should give path as那么你应该给出路径

 <div>
      <img  src="assets/images/New-Google-Logo.png"/>
 </div>

When you serve the project all static assets needs to be served to client in order to display it on client.当您为项目提供服务时,所有静态资产都需要提供给客户端,以便在客户端上显示。 Angular cli build and bundle entire project in js files. Angular cli 在 js 文件中构建和捆绑整个项目。 To serve your static assets you can follow two ways要为您的静态资产提供服务,您可以通过两种方式

  • put all your static assets in assets folder which angular-cli serves with default .angular-cli.json将所有静态资产放在 angular-cli 使用默认 .angular-cli.json 服务的资产文件夹中
  • or you need to make entry of the static assets folder in .angular-cli.json file in array named as assets as my images folder is in static folder and static folder is at same hierarchy level of assets folder或者您需要在名为 assets 的数组中的 .angular-cli.json 文件中输入静态资产文件夹,因为我的图像文件夹位于静态文件夹中,而静态文件夹位于资产文件夹的相同层次结构级别

    "assets": [ "assets", "static/images" ]

change path to将路径更改为

<img  src="assets/images/New-Google-Logo.png"/>

and add height and width并添加高度和宽度

 <img  src="assets/images/New-Google-Logo.png" width="200" height="100">

If you create project using CLI you will have one assets folder just create one image folder inside it and paste your all images inside it of .png type then give如果您使用 CLI 创建项目,您将拥有一个资产文件夹,只需在其中创建一个图像文件夹,然后将所有图像粘贴到其中的 .png 类型,然后给出
reference src="assets/images/AnyImage.png" inside your html it will load the images.在 html 中引用 src="assets/images/AnyImage.png" 它将加载图像。 Actually you pasted your images inside loginComponent folder.实际上,您将图像粘贴到 loginComponent 文件夹中。 Try this it will work.试试这个它会起作用。

if you have references like this in your css files where images are referenced as below如果您的 css 文件中有这样的引用,其中引用的图像如下

    .topmenu a:hover {
    background:url('../images/topm_bg_right.gif') right top no-repeat;
    color:#fff;
   }
 then you may have to add the image extension wild cards in the permit all list of your 
 WebSecurityConfig class as shown.. especially when you have bundled your app with both angular and spring boot together ng build and moved to the static folder

 .antMatchers("/","/*.jpg","/*.png",
                    "/home","/assets/**",
                    "/polyfills.js",).permitAll()

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

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