【问题标题】:Angular 2 application with no server?没有服务器的Angular 2应用程序?
【发布时间】:2017-02-22 15:38:18
【问题描述】:

如何在不运行服务器的情况下运行 Angular 2 应用程序? 我只需要一个包含 index.html 的文件夹(另外还有一些 css、js)

【问题讨论】:

标签: node.js angular typescript


【解决方案1】:

如果您使用 angular-cli,您可以使用 ng build --prod。 index.html 将在 dist 文件夹中。

【讨论】:

  • 我确实像你说的那样。但是当我从 dist 文件夹运行 index.html 时,我得到一个空白页面,顶部显示“正在加载...”消息。问题出在哪里?
  • 您需要从服务器运行index.html,而不仅仅是在浏览器中打开文件。查看浏览器控制台,index.html 尝试加载的所有 js/css 文件可能都出现 404 错误。
  • 所以我不能在不运行服务器的情况下运行我的 Angular2 应用程序?
  • @ArtemyWiese 你可以,按照 al37350 说的做,但要解决你的错误编辑创建的 index.html 文件并用 更改 也是它重要的是您的路由器策略是位置,而不是哈希。
【解决方案2】:

完成 Admir Sabanovic 的回答。在您的 app.module.ts 中添加为提供者

providers: [
   { provide: LocationStrategy, useClass: HashLocationStrategy }
]

在您的 index.html 中将 行更改为

<script>document.write("<base href='"+window.location.href+"'/>") </script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2023-03-24
    相关资源
    最近更新 更多