我想用5000而不是4200。
我尝试在根名称ember-cli上创建一个文件,并按照下面的代码放置JSON:
{
"port": 5000
}
但我的应用程序仍然运行在4200,而不是5000
我想用5000而不是4200。
我尝试在根名称ember-cli上创建一个文件,并按照下面的代码放置JSON:
{
"port": 5000
}
但我的应用程序仍然运行在4200,而不是5000
当前回答
虽然上面的答案中已经有很多有效的解决方案,但这里是一个使用visual Studio Code的Angular 7项目的可视化指南和具体解决方案(也可能是更早的版本,但不保证)。定位模式。在图像树中显示的目录中添加Json,并更改port——> default下的整数,以永久更改浏览器中的端口。
其他回答
要在运行时更改一次端口,并且没有更改配置,您可以使用以下命令
ng serve --port 5000
如果每次运行ng服务都需要5000端口。使用以下方法
使用angular.json
在“选项”字段下搜索“服务”,如下所示:
在node_modules > @angular-devkit > build-angular > src > dev-server > schema中。Json,你会发现下面的代码和更新端口,如你想要的5000。 在包中。Json下的“脚本”,我们已经“开始”更新它与下面的命令,所以每次运行“npm开始”,它将服务于5000。
注意:在终端中使用'ng serve'或'npm start'发布所有的重启服务器反馈有助于改进。谢谢!
如果你有多个环境,你可以在angular.json中添加如下方式:
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "your_project:build"
},
"configurations": {
"production": {
"browserTarget": "your_project:build:production"
},
"es5": {
"browserTarget": "your_project:build:es5"
},
"local": {
"browserTarget": "your_project:build:local",
"port": 4201
},
"uat": {
"browserTarget": "your_project:build:uat"
}
}
},
这样只有本地指向另一个端口。
你也可以这样写:ng serve -p 5000
在CLI的最新版本(我使用的是6.0.1)中,情况似乎发生了变化。我可以通过在项目的angular.json中添加一个port选项来改变ng serve使用的默认端口:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"projects": {
"my-project": {
"architect": {
"serve": {
"options": {
"port": 4201
}
}
}
}
}
}
(本例中只显示相关属性。)
在Angular中,我们有两种改变默认端口号的方法。
cli命令的第一种方式:
ng serve --port 2400 --open
第二种方法是在以下位置进行配置:ProjectName\node_modules\@angular-devkit\build-angular\src\dev-server\schema.json。
在模式中进行更改。json文件。
{
"title": "Dev Server Target",
"description": "Dev Server target options for Build Facade.",
"type": "object",
"properties": {
"browserTarget": {
"type": "string",
"description": "Target to serve."
},
"port": {
"type": "number",
"description": "Port to listen on.",
"default": 2400
},