vue前端获取本地IP地址代码实例
作者:一百减一是零
再做前端页面的时候,想获取本地的ip地址,可能是为了和服务器通信,可能是为了展示,无论哪种,下面给大家总结下方法,这篇文章主要给大家介绍了关于vue前端获取本地IP地址的相关资料,需要的朋友可以参考下

新建js文件
getIpAddress.js
//获取本机的IP地址
function getNetworkIp() {
let ip = 'localhost';
try {
const network = Os.networkInterfaces();//获取本机的网路
for (const iter in network) {
const faces = network[iter];
for (const interface of faces) {
if (interface.family === 'IPv4' && interface.address !== '127.0.0.1' && !interface.internal) {
ip = interface.address;
return ip;
}
}
}
} catch (e) {
}
return ip;
}
module.exports = getNetworkIp;在index.js文件中添加以下配置
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// Various Dev Server settings
host: getNetworkIp(), // can be overwritten by process.env.HOST
useLocalIp: true,//允许使用本地IP地址进行访问
port: 18880, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
cssSourceMap: false
},总结
到此这篇关于vue前端获取本地IP地址的文章就介绍到这了,更多相关vue获取本地IP地址内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
