Angular 5 app not displaying on Github pages

爷,独闯天下 提交于 2019-12-01 22:09:08
<base href="/ng-test-deploy/">

This is needed not only for scripts and styles to load, but for router to work properly too.

Credit to both @ochs.tobi and @funkizer for their help. So yes, the base href was incorrect for the project.

First thing I had to do was to update the href in the index.html to the name of my project.

After that I needed to rebuild the application for a production environment and only have the project name as the href:

 ng build --prod --base-href "ng-test-deploy"

Then navigating to the site, after say 30 seconds, displayed what I was after.

EDIT

After investigating this some more I feel this is a better option. Inside of the angular-cli.json you can define different apps for different purposes - perhaps one for production, staging etc. Like so:

"apps": [
    { 
      "name": "app-dev",
      "root": "src",
      "outDir": "dist",
      "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.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    },
    {
      "name": "app-production",
      "baseHref": "/ng-test-deploy",
      "root": "src",
      "outDir": "dist",
      "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.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],

Inside each of these objects you can see the base-href to whatever you like. Then to build it out, in this case the app-production app, do this:

ng build --prod --base-href "ng-test-deploy" --app app-production

Which will build the target app for production based on its href. Leaving out the base href tag above resulted in the same error as I described above and is needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!