如何在Angular 6中的本地机器上运行Dist文件夹?

Jig*_*try 25 typescript angular angular6

我正在Angular中构建应用程序,现在我运行了命令ng build --prod,它给了我一个dist文件夹.现在如何在Localhost上检查或提供该文件夹?

Anu*_*ara 44

你可以使用http-server来做到这一点.

首先全局安装包

npm install http-server -g.

然后在项目目录内(在终端中)运行

http-server dist/.

如果你使用角度6.xx,你必须运行

http-server dist/your-project-name

现在,您可以访问http:// localhost:8080来查看您的应用程序

  • 如果你安装了 `npm` > 5.2,那么你可以使用 `npx` 在没有全局安装的情况下执行与上面相同的操作。例如`npx http-server dist/`。每次执行时都会下载并运行二进制文件,因此对于一次性或不经常使用的情况,它非常好。如果您经常使用该软件包或离线时需要,请按照答案中的步骤操作。 (5认同)
  • 当我尝试在192.168.2.36:8080或localhost:8080端口中运行时,我在应用程序中使用angular 7.xx,应用程序未运行。并显示“此页面无效。”。如果您有任何建议,请告诉我。提前致谢 (4认同)
  • 当我导航到路线后重新加载浏览器时,它不起作用。有什么建议么 ? (2认同)
  • @Santhosh 的同样问题,http-server 提供了一系列可以找到该应用程序的地址,但它们都不起作用... (2认同)

小智 7

就我而言,我接下来要做的是:

全局安装http-server

npm install http-server -g

然后在项目目录中(在终端中),运行

http-server dist/[your-project-name]

现在,您可以访问http:// localhost:8080 / index.html来查看您的应用程序。但是,当我重新启动浏览器页面时,我必须再次将/index.html添加到URL中。


它可以在Angular 7.xx版本中使用。


R M*_*hed 7

从终端:
$ npm install http-server -g

来自 Angular 项目目录。假设您的 Angular 项目名称是 ngx。
$ ng build
$ http-server dist/ngx
启动 http-server,服务 dist/ngx
可用于:
http://127.0.0.1:8080
http://192.168.43.54:8080
按 CTRL-C 停止服务器

享受!


mik*_*ike 7

编辑:Angular 12+: ng serve --configuration production

从 Angular 7 开始,你可以只做ng serve --prod=true. 文档:https : //angular.io/cli/serve

我知道这个问题是关于 Angular 6 的,但 Google 把我带到了这里,所以仅供将来参考。