Angular 2 CLI 项目在部署时无法加载资源

标签 angular web-deployment github-pages angular-cli

我的机器和开发环境是: 苹果电脑10.11.4 Angular -cli:1.0.0-beta.10 节点:5.11.1 操作系统: Darwin x64

该项目是从 Angular CLI 创建的。该应用程序在运行“ngserve --prod”时工作正常,但是当我运行“ng build --prod”然后实时发布时,我会出现大量找不到资源的错误。

enter image description here

在index.html中加载这些资源有很多“魔力”,所以我不知道如何解决这个问题......

https://github.com/JimTheMan/Safe-Foods/tree/gh-pages

我的项目名为“SafeFoods”。例如 es6-shim.js 的正确路径应该是: https://jimtheman.github.io/Safe-Foods/vendor/es6-shim/es6-shim.js 当它实际上是: https://jimtheman.github.io/vendor/es6-shim/es6-shim.js

ng 构建任务每次都会这样创建它,所以我不确定如何告诉它正确的路径...

注意:我还尝试将 angular-cli 更新到最新版本并使用“--base-href”,但这似乎无法正常运行:

SafeFoods (gh-pages) ♔ ng build --prod --base-href/Safe-Foods/

选项“--base-href”未在构建命令中注册。运行 build --help 以获取受支持选项的列表。

感谢任何帮助。谢谢。

最佳答案

config/environment.js设置baseURL: '/Safe-Foods' .

src/index.html设置<base href="{{ baseURL }}"> .

关于Angular 2 CLI 项目在部署时无法加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39334259/

相关文章:

Angular 4 - 我可以使用 ngFor 动态创建表单吗?

java - 如何在 Mac 上设置 gradle

html - 将自定义字体添加到 GitHub 页面

Github 页面 - 禁用除单个站点之外的所有站点的自定义域重定向?

angular - 在 angular2 中使用 ng2-charts 的水平条形图

javascript - 如何从 Angular 6 中的 HttpErrorResponse 获取正文?

Angular2 HashLocationStrategy 总是重定向到 root

browser - 发布asp.net应用程序后,如何防止Visual Studio 2015启动浏览器?

javascript - 如何将两个独立服务中的 js 缩小到在前端协同工作

html - 使用 meta http-equiv ="refresh"和 gh-pages 的相对重定向