在没有服务器的浏览器中将 Angular2 作为静态应用程序运行 [英] Run Angular2 as static app in browser without a server

查看:27
本文介绍了在没有服务器的浏览器中将 Angular2 作为静态应用程序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解 Angular2 概念 - 它将 TypeScript 文件转换为 .js 文件.原则上,应该可以从 AWS S3 存储桶、GitHub 或任何静态源编译、打包该 Angular2 应用程序,然后将其作为静态应用程序运行.

As I understand the Angular2 concept - it is transpiling TypeScript files to .js files. In principle, it should be possible to compile, package, and then run that Angular2 application as a static application from AWS S3 bucket, GitHub or whatever static source.

如果我在节点服务器上运行 Angular2 应用程序(使用 angular-cli "ng serve" 命令),它在服务器上需要 500 MB 的 RAM - 这是嘿,常见!"——真的是这样吗!这个框架相对于 React 来说有什么好处,例如,它只需要一个浏览器.

If I run Angular2 application on node server (with angular-cli "ng serve" command), it takes 500 MB of RAM on server - it's "Heey, common!" - is it really supposed to be that way! What's the benefit of this framework then against React, for example, which needs only a browser.

对于将 Angular2 应用程序作为静态编译的 HTML+JS 提供服务,我似乎找不到任何有用的东西.

I can't seem to find anything useful on serving Angular2 application as a static compiled HTML+JS.

也许你能帮助我理解这一点并解决?

Maybe you can help me understand this, and solve?

非常感谢!

马里斯

推荐答案

运行 BUILD 命令BUNDLE/build

ng 构建

或用于生产构建/捆绑

ng build --prod

ng build --prod

它会将您的应用构建/捆绑成一个可分发的应用.

It will build/bundle your app into a distributable app.

完成后,在您的应用程序根目录中查找 dist 文件夹,该文件夹将包含您的应用程序在节点服务器外部运行所需的所有内容,例如 tomcat 实例.

When it is finished look in your apps root directory for a dist folder and that will contain everything your app needs to run in outside of the node server, say like a tomcat instance.

更新

感谢@Maris 的评论,请确保您的文件路径相对于当前目录而不是相对于根目录.

Thanks to the comment from @Maris, make sure your file paths are relative to the current directory rather than relative to the root directory.

只需运行此命令即可更改 index.html 中的基本 href 元素.

Simply run this command to change the base href element in your index.html.

ng build --prod --base-href ./

ng build --prod --base-href ./

这篇关于在没有服务器的浏览器中将 Angular2 作为静态应用程序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆