今天来解决一个问题:基于Vite构建的Vue项目在启动后只能通过localhost这种形式访问
如果把localhost换成本主机的局域网ip地址之后页面无法访问了。
就连用127.0.0.1都无法访问。尝试多次之后,最后证明只有使用localhost这种形式才可以
![](https://img-blog.csdnimg.cn/direct/92a8d78974f241d1b843873c9ebcf28c.png)
原因:基于Vite构建的Vue项目在一开始创建后,默认设置的是只能用localhost这种形式来访问
![](https://img-blog.csdnimg.cn/direct/e5ea941f2cbb4ee5815f57a2dbe9937b.png)
想要真正实现让不同的设备能够访问我们做的web时,就需要解决这个问题,localhost 替换成本机 ip 地址之后无法访问。
解决措施:
在vite.config.js文件中设置如下
![](https://img-blog.csdnimg.cn/direct/a08ca30b4e124ea682b3a19adca10a60.png)