因为需要让局域网内其他人也能访问到自己的项目,所以必须要使用局域网IP来访问。但是发现只有localhost或者127.0.0.1能访问到项目,局域网内的IP死都不行。

后来搜索了一把,发现其实很简单就能解决。

只用把vue-cli项目下的config文件夹下面的index.js文件里的localhost改为0.0.0.0就可以了。

这样修改也不妨碍127.0.0.1或者localhost的访问。

  1. // Various Dev Server settings
  2. host: '0.0.0.0', // can be overwritten by process.env.HOST


答案参考自:https://segmentfault.com/q/1010000012132756/a-1020000012134067