Angular 5 应用程序未显示在 Github 页面上

Kat*_*a24 5 github-pages angular angular-cli-ghpages

我使用 CLI 创建了默认的 Angular 应用程序,如下所示:

ng new ng-test-deploy
Run Code Online (Sandbox Code Playgroud)

测试将虚拟应用程序部署到 Github 页面。我完成了此处定义的程序,总而言之,它执行以下操作:

ng build --prod --base-href "https://USERNAME.github.io/REPOSITORY_NAME/"
angular-cli-ghpages
Run Code Online (Sandbox Code Playgroud)

这样做之后,我访问了该页面并收到一个空白页面。在控制台中,加载文件时出现多个错误:

在此处输入图片说明

如果我检查 say 的 url,main.bundle.js我可以看到路径指向https://katana24.github.io/main.3188de4880a80d258168.bundle.js 而我可以通过将 repo 名称添加到 url 中来加载它,如下所示: https://katana24.github.io/ng-test-edeploy/main.3188de4880a80d258168.bundle.js

我搞砸了一步吗?

先前问题的答案建议删除该base-href="..."值,但这对我来说没有意义。那么我如何让它看起来在正确的地方呢?

Kat*_*a24 6

感谢@ochs.tobi 和@funkizer 的帮助。所以是的,base href该项目不正确。

我必须做的第一件事是将hrefin更新index.html为我的项目名称。

之后,我需要为生产环境重建应用程序,并且只有项目名称为href

 ng build --prod --base-href "ng-test-deploy"
Run Code Online (Sandbox Code Playgroud)

然后导航到该站点,例如 30 秒后,显示了我想要的内容。

编辑

在对此进行了更多调查后,我觉得这是一个更好的选择。在里面angular-cli.json你可以为不同的目的定义不同的应用程序 - 也许一个用于生产,登台等。像这样:

"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"
      }
    }
  ],
Run Code Online (Sandbox Code Playgroud)

在这些对象中的每一个中,您都可以看到base-href您喜欢的任何内容。然后要构建它,在这种情况下是app-production应用程序,请执行以下操作:

ng build --prod --base-href "ng-test-deploy" --app app-production
Run Code Online (Sandbox Code Playgroud)

这将根据其 href 构建用于生产的目标应用程序。省略上面的基本 href 标签会导致与我上面描述的相同的错误,并且是必需的。