Vue 自動獲取本地ip,並打開瀏覽器

我的原創地址:https://dongkelun.com/2019/03/28/vueAutoIpAndOpenBrowser/

前言

功能如題,本文參考:https://www.jianshu.com/p/54daac2cc924,目的只是爲了把網上查的資料做個筆記~

以下均爲vue cli2 創建的項目

自動打開瀏覽器

只需要在config/index.js裏找到autoOpenBrowser將其設爲true即可

獲取本地ip

方法一

在config/index.js 頂部添加

const os = require('os')
let localhost = ''
try {
  const network = os.networkInterfaces()
  localhost = network[Object.keys(network)[0]][1].address
} catch (e) {
  localhost = 'localhost';
}


再找到host將其改爲host:localhost即可

效果代碼查看:https://github.com/dongkelun/vue-echarts-map/blob/autopip-v1/config/index.js

方法二

安裝address

npm i address -D

在config/index.js

const address = require('address')
const localhost = address.ip() || 'localhost'

再找到host將其改爲host:localhost即可

效果代碼查看:https://github.com/dongkelun/vue-echarts-map/blob/master/config/index.js

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章