Angular2, Angular-cli, Webpack - images do not load on github: pages after deploying prod


This is not a duplicate question:

This question explains the problem, but this project was not built with angular-cli, since mine, therefore, I do not have a file webpack.config.

While the problem I am talking about below clearly indicates that base_url is configured correctly, this is not the same as this problem , which is setting angular to send requests to base_url as an api.


My project is built and launched on localhost, as well as on github:pages(even css everything loads correctly). The only thing that does not work on github: pages are images that return 404s (even if I can see them, if I go to the resource folder directly in the browser).

The problem is that on localhost angular-cli starts the project without a subfolder after the host name. But on github:pageshe does.

Let's say, for example, the name of the project testapp, and then github:pagesit is expanded here:

https://<username>.imtqy.com/testapp

Images are in

src/assets/images

They reference css inside the angular -2 component, using relative paths as follows:

background-image: url('../../assets/images/dining-room.png');

So, how do I configure the angular -2 production deployment file so that during the creation process the path becomes:

background-image: url('testapp/assets/dining-room.png');

, github:pages Jekyll, , , angular -cli, , github:pages .


1

, github:pages base_href. :

ng github-pages:deploy --base-href /testapp/

/dist, html , href, :

<base href="/testapp/">

, , 404. base_href , . index.html <base href="/"> , , .


2

, , , angular . .


EDIT angular -cli.json
{
  "project": {
    "version": "1.0.0-beta.22-1",
    "name": "floorbook"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "fb",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}
+4
2

, Angular2 , . , , ng / , ROOT. . UPDATE 2 .

, CSS 2 app/src. :

../../assets/images/<file>.png

angular builds /assets/images ROOT - DIST (.. ng github-pages:deploy)

, , , css root (.. , css ):

assets/images/<file>.png

:

ng github-pages:deploy --base-href /testapp/

, /testapp/ , , :

/testapp/assets/images/<file>.png


, , - APP_BASE_HREF

, , , , base-href . , , , :

1 - app.module.ts :

import { APP_BASE_HREF } from '@angular/common';

providers: [{provide: APP_BASE_HREF, useValue : '/your-value/'}]

- , - href. , index.html ( ng build , index.html DIST).

2 - 2 APP_BASE_REF :

import { APP_BASE_HREF } from '@angular/common';
import { Inject } from '@angular/core';

constructor(@Inject(APP_BASE_HREF) private baseHref:string) { 
  console.log(this.baseHref);
}

1 , :

useValue : '/'

github: pages... , , ( ... ).

30 2016 . .

+9

localhost dev. , SCSS, . , : background: url(../../assets/heros/contact-hero-mbl@2x.jpg);. : background: url(/assets/heros/contact-hero-mbl@2x.jpg); , , . , -.

+1

Source: https://habr.com/ru/post/1665240/


All Articles