如何在Vue-cli项目中更改端口号,使其在另一个端口上运行而不是8080。
当前回答
如果您正在使用vue cli 3,另一个选择是使用配置文件。创建vue.config.js与你的包在同一级别。Json,并像这样放置配置:
module.exports = {
devServer: {
port: 3000
}
}
使用脚本配置:
npm run serve --port 3000
工作很好,但如果你有更多的配置选项,我喜欢在配置文件中做。你可以在文档中找到更多信息。
其他回答
在撰写本文时(2018年5月5日),vue-cli的配置托管在<your_project_root>/vue.config.js。要更改端口,请参见以下内容:
// vue.config.js
module.exports = {
// ...
devServer: {
open: process.platform === 'darwin',
host: '0.0.0.0',
port: 8080, // CHANGE YOUR PORT HERE!
https: false,
hotOnly: false,
},
// ...
}
vue.config.js的完整参考可以在这里找到:https://cli.vuejs.org/config/#global-cli-config
请注意,正如文档中所述,“webpack-dev-server的所有选项”(https://webpack.js.org/configuration/dev-server/)在devServer部分中可用。
在visual studio代码中的vue项目中,我必须在/config/index.js中设置这个。 在下面更改:
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
host: 'localhost', // can be overwritten by process.env.HOST
port: 8090, // 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
}
}
最好的方法是更新包中的serve脚本命令。json文件。只需像这样追加——port 3000:
"scripts": {
"serve": "vue-cli-service serve --port 3000",
"build": "vue-cli-service build",
"inspect": "vue-cli-service inspect",
"lint": "vue-cli-service lint"
},
在webpack.config.js中:
module.exports = {
......
devServer: {
historyApiFallback: true,
port: 8081, // you can change the port there
noInfo: true,
overlay: true
},
......
}
您可以在模块中更改端口。exports -> devServer ->端口。
然后你重新启动npm run dev。你可以得到那个。
打开package.json 添加名为serve的脚本,"serve": "Vue-cli-service serve -port 8081" NPM运行服务 服务器将运行8081
{
"name": "app-name",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve --port 8081",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
}
}
推荐文章
- 如何在vue-cli项目中更改端口号
- Angular 2模板中的标签是什么意思?
- JavaScript .includes()方法的多个条件
- 窗口。亲近与自我。close不关闭Chrome中的窗口
- 同步和异步编程(在node.js中)的区别是什么?
- 在d3.js中调整窗口大小时调整svg的大小
- 如何编辑通过npm安装的节点模块?
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check