1.修改 package.json,增加 --host 0.0.0.0,让本次代码支持localhost以外的ip地址访问模式
2.修改config/index.js,将localhost改为0.0.0.0
此时项目就可以通过localhost或本机IP访问了,但是在cmd输出上只提示了0.0.0.0:8080访问路径,需要修改提示
修改方式:
1.在build目录下创建一个获取本机IP的方法,get-ip.js
var os = require('os'),ip = '',ifaces = workInterfaces() // 获取本机ipout:for (var i in ifaces) {for (var j in ifaces[i]) {var val = ifaces[i][j]if (val.family === 'IPv4' && val.address !== '127.0.0.1') {ip = val.addressbreak out}}}
module.exports = ip
2.在webpack.dev.conf.js中引用方法
const IP = require('./get-ip')
3.修改提示
` App running at: `,`-Local: http://localhost:${port}`,`-Network: http://${IP}:${port}`
4.最后