简体   繁体   中英

Angular CLI - Images not showing after 'ng build'

When I run ng build I have a problem. I try to load some image to my page, but these images are not shown. My console not is not throwing any 404 errors. All of my images are place in assets folder.

<div>
    <h1>Home</h1>
    <img src="assets/img/people/user-1.png" alt="user" />
</div>

But if I run command ng serve it's works fine. Thank you for all your answers.

Here is my .angular.cli.json file

`{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "frontend"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "../src/main/webapp",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {}
  }
}`

I think you have the problem with the server. If you are trying to open your page by click on index.html, it will work only with js files without assets.

How do you start your server after build?

It happened the same to me, and the problem was that I was forwarding requests on the server , so that the user was able to enter the application in a specific route. (I mean, for example, http://myangularapp.com/profile )

In my case, I built a jar with both a spring boot backend and an angular frontend and therefore needed that trick to allow angular to resolve the routes.

I believe something changed recently with angular-cli or angular, since I already used that strategy before and it was working fine.

By the way, this happened with "@angular/cli": "^1.2.0".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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