我想用5000而不是4200。

我尝试在根名称ember-cli上创建一个文件,并按照下面的代码放置JSON:

{
   "port": 5000
}

但我的应用程序仍然运行在4200,而不是5000


当前回答

修改nodel_modules/angular-cli/commands/server.js是个坏主意,因为当你安装新版本的angular-cli时,它会被更新。相反,你应该在package中指定ng serve——port 5000。Json是这样的:

"scripts": {
    "start": "ng serve --port 5000"
}

你也可以用——host 127.0.0.1指定主机

其他回答

简单设置在包中。json文件

"scripts": {
    "ng": "ng",
    "start": "ng serve --port 3000",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

然后运行命令

npm start

转到angular。json文件,

搜索关键字“serve”:

添加port关键字如下所示:

 "serve": {
     "builder": "@angular-devkit/build-angular:dev-server",
     "options": {
         "browserTarget": "my-new-angular-app:build",
         "port": 3001
     },
     ...

对我来说有效的解决方法是

ng serve --port 4401    

(你可以把4401改成任何你想要的号码)

然后启动浏览器-> http://localhost:4401/

基本上,我有两个应用程序,在上述方法的帮助下,现在我能够在我的开发环境中同时运行它们。

如果你有多个环境,你可以在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"
        }
      }
    },

这样只有本地指向另一个端口。

虽然上面的答案中已经有很多有效的解决方案,但这里是一个使用visual Studio Code的Angular 7项目的可视化指南和具体解决方案(也可能是更早的版本,但不保证)。定位模式。在图像树中显示的目录中添加Json,并更改port——> default下的整数,以永久更改浏览器中的端口。