如何从 Vagrant box 运行 angular-cli?

How to run angular-cli from Vagrant box?

我已经在我的 vagrant (Scotch) 盒子上安装了 angular-cli with node v6.5.0 和 npm v3.10.3。在 运行 'ng serve --port 4201' 之后,我可以看到构建成功并且应用程序现在在 http://localhost:4201/

上提供服务

但是我不能只使用上面的地址,因为我在 VM 上 运行。我试图将“127.0.0.1:4201”添加到主机上的主机文件中,但无法正常工作。

谢谢

您必须向 Vagrantfile 添加一些配置。

要访问站点,请添加这些行(这对我有帮助):

config.vm.network "private_network", ip: "192.168.100.100"
config.vm.network "forwarded_port", guest: 4200, host: 4200
config.vm.network "forwarded_port", guest: 49152, host: 49152

您可以访问网站:http://192.168.100.100:4200/

Vagrant (VM) 不会触发文件更改事件。此错误已报告给 VirtualBox 社区并设置为不会修复。目前无法通过 Vagrant 使用 Angular 2。有关这方面的相关讨论,请参阅 this issue on GitHub

运行 ng serve 像这样:

ng serve --host 0.0.0.0

这将使您的 vagrant 机器中的服务器使用其 ip(我的是 192.168.10.10 使用 homestead)和 angular serve 的默认 4200 端口(http://192.168.10.10:4200 ).

更多信息:

  • :

127.0.0.1 is a loopback address so if you assign this IP (or localhost) to the config of your server, its not accessible from other network interfaces, so you cannot access it from outside of the VM. If you bind your server to 0.0.0.0 its available to all interfaces

您可以通过多种方式做到这一点, 对于我自己,我首先安装了 VMware(虚拟机),确保你获得了最新版本。

安装vm成功后,下载nodejs和vagrant。 打开 cmd >> 将它定位到你的 vagrant 所在的文件夹, 接下来输入 Vagrant up 它应该 运行 .. 如果您遇到更多问题。尝试键入这个 vagrant ssh [名称|id] [-- extra_ssh_args]..

希望对您有所帮助