我的原创地址: 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
来源: http://www.jianshu.com/p/8397760571aa