3
votes

I use Angular 6

index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My App</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>

<script src="./config.js"></script>
</body>
</html>

Also, the template of one component contains the img tag:

<img src="assets/spinner.gif">

In case I build it with the base-href/deploy-url parameters all works ok (base href, all css files and so on), except paths to config.js and assets/spinner.gif - angular-cli keeps these paths without changes. How to solve this?

PS. I tried to use "assets/spinner.gif", "/assets/spinner.gif", "./assets/spinner.gif" - with the same result.

PPS Fragment of angular.json:

    {
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "project_name": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/project_name",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/web.config",
              "src/config.js",
              "src/assets"
            ],
1
Can you add your angular.json file please? specifically, the section under build -> options?Mark Hughes

1 Answers

5
votes

When using the base-href parameter, it is important to always ensure that the path used ends with a trailing / and the paths in the HTML do not start with a / - so you need to end up with:

<base href="/dir/"/>

and

<img src="assets/spinner.gif"/>

Without the trailing /, the base will not be applied correctly.