6

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

ng new ng-test-deploy

测试将虚拟应用程序部署到 Github 页面。我运行了此处定义的程序,总而言之,它是这样做的:

ng build --prod --base-href "https://USERNAME.github.io/REPOSITORY_NAME/"
angular-cli-ghpages

之后,我访问了该页面并收到一个空白页面。在控制台中,有关加载文件的多个错误:

在此处输入图像描述

如果我检查说的 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="..."值,但这对我来说没有意义。那么如何让它在正确的地方找到呢?

4

2 回答 2

6

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

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

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

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

然后导航到该站点,说 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"
      }
    }
  ],

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

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

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

于 2018-03-05T13:25:47.007 回答
4
<base href="/ng-test-deploy/">

这不仅需要加载脚本和样式,还需要路由器正常工作。

于 2018-03-04T08:51:14.373 回答