前提条件
- 本机 IP:
localhost
127.0.0.1
192.168.1.5
172.25.25.9
- 程序端口:
5173
说明
- 程序启动没有指定监听 IP 的情况下,默认监听本机所有的 IP,即:可以使用
127.0.0.1:端口、本机任意一个内网IP:端口 访问程序,比如:localhost:5173、127.0.0.1:5173、192.168.1.5:5173、172.25.25.9:5173
- 程序启动指定监听 IP 的情况下,只能通过
监听的IP:端口 访问程序,比如:程序启动时,仅监听了127.0.0.1,只能通过 127.0.0.1:5173 访问程序,使用 192.168.1.5:5173、172.25.25.9:8080 无法访问程序
示例
- 创建项目命令
npm add -D vitepress@next
npx vitepress init
- 启动项目命令
npm run docs:dev
- 由于
vitepress 默认仅仅监听 localhost,所以只能使用 http://localhost:5173 访问 vitepress 程序,而不能使用 http://127.0.0.1:5173 访问程序
- 在
package.json 中的 scripts 添加 "docs:dev:host": "vitepress dev --host",然后使用 npm run docs:dev:host 启动程序,就可以使用本机任意 IP 访问 vitepress 了,比如:
- http://localhost:5173
- http://127.0.0.1:5173
- http://192.168.1.5:5173
- http://172.25.25.9:5173