【问题标题】:Angular 5 app not displaying on Github pagesAngular 5 应用程序未显示在 Github 页面上
【发布时间】:2018-03-05 23:11:30
【问题描述】:

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

ng new ng-test-deploy

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

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

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

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

我是不是搞砸了一步?

以前的questions' 回答建议删除base-href="..." 值,但这对我来说没有意义。那么如何让它出现在正确的位置呢?

【问题讨论】:

  • 能否为您的应用添加index.html
  • 我认为你搞砸了基本的 href。不要将整个 URL 放在那里。只有上下文“ng-test-deploy”。

标签: angular github-pages angular-cli-ghpages


【解决方案1】:

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

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

之后我需要为生产环境重新构建应用程序,并且项目名称只有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 标记会导致与我在上面描述的相同的错误,并且是必需的。

【讨论】:

    【解决方案2】:
    <base href="/ng-test-deploy/">
    

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

    【讨论】:

    • 我实际上怀疑这一点。很快就会测试出来
    • 那行不通。加上当地停止工作 - 原因很明显
    • 现在可以工作了 - 请参阅我的答案。标记为这是答案的一半
    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2021-08-25
    • 2023-03-24
    • 2020-04-23
    相关资源
    最近更新 更多