1.在 config 文件里的 index.js 里面加上以下代码:

//config/index.js
const os = require('os');

function getNetworkIp() {
	let needHost = ''; // 打开的host
	try {
		// 获得网络接口列表
		let network = os.networkInterfaces();
		for (let dev in network) {
			let iface = network[dev];
			for (let i = 0; i < iface.length; i++) {
				let alias = iface[i];
				if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
					needHost = alias.address;
				}
			}
		}
	} catch (e) {
		needHost = 'localhost';
	}
	return needHost;
}

2.然后将 dev 里的 host 改成 getNetworkIp() 即可,代码如下;

dev: {
	assetsSubDirectory: 'static',
	assetsPublicPath: '/',
	proxyTable: proxy,
	host: getNetworkIp(),
	port: 9528,
	autoOpenBrowser: true,
	errorOverlay: true,
	notifyOnErrors: false,
	poll: false,
	useEslint: true,
	showEslintErrorsInOverlay: false,
	devtool: 'source-map',
	cssSourceMap: false
}

然后运行项目时,控制台就会显示项目在局域网IP运行的地址

Logo

前往低代码交流专区

更多推荐