Angular 6+ 如何更改默认端口 4200
Angular 6+ How to change default port 4200
此问题特定于 Angular 的第 6 版,而不是更早的版本,因为 .angular-cli.json 文件已被替换angular.json 文件。
我创建了一个新的 Angular 6 项目,并像以前的版本一样尝试更改其默认端口,但这次是 angular.json。
"defaults": {
"serve": {
"port": 4220
},
但是出现如下错误:
Invalid schema detected in .angular-cli.json, please correct and try
again!
有人知道如何使用新版本的 Angular 做到这一点吗?
由于标题不准确:“angular-cli server - how to specify default port”,很难找到我的问题的答案,但感谢 Vladymir Gonzalez 我找到了。
为了帮助其他人快速找到答案,我在这里提取了属于elwyn的Angular6的具体部分:
@angular/cli@6.x 的更新:在新的 angular.json 中,您现在可以为每个 "project"
指定一个端口
"projects": {
"my-cool-project": {
... rest of project config omitted
"architect": {
"serve": {
"options": {
"port": 4850 <-- add your custom port number here
}
}
}
}
}
所有可用选项:
https://github.com/angular/angular-cli/wiki/angular-workspace
您始终可以在服务时指定端口:ng serve --port 3000
您可以将任何有效的端口号放在那里,它将从该端口提供服务。
我正在回答 angular 2+ 申请。
如果您想在不同端口启动 2 angular 应用程序,即
1) port 4200
2) port 5000
你需要更改 "package.json" 文件,只需更改在第一个花括号中添加一行 "Script block"其中您的应用程序名称,版本默认可用,
"scripts": {
"ng": "ng",
"start": "ng serve --port 5000 ",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
通过"npm start"命令启动您的应用程序。
此命令将在 Port 5000.
上启动您的应用程序
更改默认端口的两种方法
第一:使用命令
ng serve --port 8000 || ng serve --host '192.168.1.1' --port 8000
其次:编辑您的 package.json
"scripts": {
"ng": "ng",
"start": "ng serve --port 8000",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
如果您在一段时间内在您的系统中使用多个项目,那么条件第一个是最好的,我首先对练习有好处
答案很简单
在命令提示符中写入 ng serve --open --port=YourPortNumber
。
示例:
ng serve --open --port=4201
==== 其他解决方案 ====
您也可以在 package.json
中更改它
"scripts": {
"start": "ng serve --open --port=4201",
}
现在,在命令提示符中只写 npm start
到 运行 angular 在指定端口上使用命令 sudo ng serve --port 8080
代替 8080 您可以指定任何端口号。
这只会运行端口号上的项目,除非您关闭它。
此问题特定于 Angular 的第 6 版,而不是更早的版本,因为 .angular-cli.json 文件已被替换angular.json 文件。
我创建了一个新的 Angular 6 项目,并像以前的版本一样尝试更改其默认端口,但这次是 angular.json。
"defaults": {
"serve": {
"port": 4220
},
但是出现如下错误:
Invalid schema detected in .angular-cli.json, please correct and try again!
有人知道如何使用新版本的 Angular 做到这一点吗?
由于标题不准确:“angular-cli server - how to specify default port”,很难找到我的问题的答案,但感谢 Vladymir Gonzalez 我找到了。
为了帮助其他人快速找到答案,我在这里提取了属于elwyn的Angular6的具体部分:
@angular/cli@6.x 的更新:在新的 angular.json 中,您现在可以为每个 "project"
指定一个端口"projects": {
"my-cool-project": {
... rest of project config omitted
"architect": {
"serve": {
"options": {
"port": 4850 <-- add your custom port number here
}
}
}
}
}
所有可用选项:
https://github.com/angular/angular-cli/wiki/angular-workspace
您始终可以在服务时指定端口:ng serve --port 3000
您可以将任何有效的端口号放在那里,它将从该端口提供服务。
我正在回答 angular 2+ 申请。
如果您想在不同端口启动 2 angular 应用程序,即
1) port 4200
2) port 5000
你需要更改 "package.json" 文件,只需更改在第一个花括号中添加一行 "Script block"其中您的应用程序名称,版本默认可用,
"scripts": {
"ng": "ng",
"start": "ng serve --port 5000 ",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
通过"npm start"命令启动您的应用程序。 此命令将在 Port 5000.
上启动您的应用程序更改默认端口的两种方法 第一:使用命令
ng serve --port 8000 || ng serve --host '192.168.1.1' --port 8000
其次:编辑您的 package.json
"scripts": {
"ng": "ng",
"start": "ng serve --port 8000",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
如果您在一段时间内在您的系统中使用多个项目,那么条件第一个是最好的,我首先对练习有好处
答案很简单
在命令提示符中写入 ng serve --open --port=YourPortNumber
。
示例:
ng serve --open --port=4201
==== 其他解决方案 ====
您也可以在 package.json
"scripts": {
"start": "ng serve --open --port=4201",
}
现在,在命令提示符中只写 npm start
到 运行 angular 在指定端口上使用命令 sudo ng serve --port 8080
代替 8080 您可以指定任何端口号。
这只会运行端口号上的项目,除非您关闭它。