VUE-获取本地局域网IP地址
vue获取本地局域网IP地址
·
const os = require('os'); //提供本地的相关信息
// 获取本地ip
function getNetWorkIp() {
// 打开host
let needHost = '';
try {
let network = os.networkInterfaces();
for (const dev in network) {
let iface = network[dev];
for (let i = 0; i < iface.length; i++) {
const alias = iface[i];
if (
alias.family === 'IPv4' &&
alias.address !== '127.0.0.1' &&
!alias.internal
) {
needHost = alias.address;
}
}
}
} catch (error) {
needHost = 'http://localhost';
}
return needHost
}
const IP = getNetWorkIp();
//跨域处理
devServer: {
host: IP, //这样就可以动态获取
proxy: {
'/api': {
target: `${IP}:3000/api`,
}
}
},
更多推荐
已为社区贡献1条内容
所有评论(0)