这个问题特定于 Angular 版本 6,而不是更早的版本,因为 .angular-cli.json 文件已被 angular.json 文件替换。
我创建了一个新的 Angular 6 项目,并尝试更改其默认端口,就像我对以前版本所做的那样,但这次是在 angular.json 中。
"defaults": {
"serve": {
"port": 4220
},
但是出现以下错误:
在 .angular-cli.json 中检测到无效模式,请更正并尝试 又来了!
有人知道如何使用这个新版本的 Angular 来做到这一点吗?
由于标题不准确:“angular-cli 服务器 - 如何指定默认端口”,很难找到我的问题的答案,但感谢 Vladymir Gonzalez 我做到了。
为了帮助其他人快速找到答案,我在这里提取了 Angular 6 的具体部分,属于 elwyn :
@angular/[email protected] 的更新:在新的 angular.json 中,您现在为每个“项目”指定一个端口
"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” 文件,只需在第一个大括号中添加一行 “脚本块”,其中您的应用程序名称、版本默认可用,
"scripts": {
"ng": "ng",
"start": "ng serve --port 5000 ",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
通过 “npm start” 命令启动您的应用程序。 此命令将在端口 5000上启动您的应用程序。
答案很简单 在命令提示符中写入
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,您可以指定任何端口号。
这只会在端口号上运行项目,除非你关闭它。
更改默认端口的两种方法 第一:使用命令
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"
},
如果您在系统中的某个时间使用多个项目,则第一个项目是最好的,并且我的第一个项目有利于练习
如果您正在运行多个 Angular 项目,则需要在不同的端口中运行该应用程序。
默认端口号:4200
端口号范围 - 1024 至 65535
运行项目时您可以提及具体的端口号
Ex : ng serve –open –port=5200
最好的选择是:
在您的 package.json 文件中, “脚本”:{ “开始”:“ng服务--端口5000” }
并运行此命令:npm run start