vue項(xiàng)目中怎么獲取當(dāng)前用戶ip?
一、在項(xiàng)目的config/index.js文件下插入如下代碼:
1 const os = require('os'); 2 3 function getNetworkIp() { 4 let needHost = ''; // 打開的host 5 try { 6 // 獲得網(wǎng)絡(luò)接口列表 7 let network = os.networkInterfaces(); 8 // console.log("network",network) 9 for (let dev in network) { 10 11 let iface = network[dev]; 12 for (let i = 0; i < iface.length; i++) { 13 let alias = iface[i]; 14 if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) { 15 needHost = alias.address; 16 // console.log("alias.address",alias.address) 17 } 18 // console.log("alias",alias) 19 } 20 } 21 } catch (e) { 22 needHost = 'localhost'; 23 } 24 return needHost; 25 }
二、在項(xiàng)目的config/index.js文件下修改host:

三、在頁(yè)面需要的地方獲取ip:
var ip = window.location.host; console.log("ip",ip.split(":")[0])
這樣就能得到我們需要的ip了。。。

浙公網(wǎng)安備 33010602011771號(hào)