angular-cli 服务器 - 如何指定默认端口 [英] angular-cli server - how to specify default port

查看:32
本文介绍了angular-cli 服务器 - 如何指定默认端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用带有 ng serve 命令的 angular-cli,如何指定默认端口,这样我就不需要每次都手动传递 --port 标志?

Using angular-cli with the ng serve command, how can I specify a default port so I do not need to manually pass the --port flag every time?

我想更改默认端口 4200.

I'd like to change from the default port 4200.

推荐答案

更新@angular/cli@9.x:及以上

angular.json 中,您可以为每个项目"指定一个端口

In angular.json you can specify a port per "project"

"projects": {
    "my-cool-project": {
        ... rest of project config omitted
        "architect": {
            "serve": {
                "options": {
                    "port": 1337
                }
            }
        }
    }
}

所有可用选项:

https://angular.io/guide/workspace-config#项目工具配置选项

或者,您可以在每次运行 ng serve 时指定端口,如下所示:

Alternatively, you may specify the port each time when running ng serve like this:

ng serve --port 1337

通过这种方法,您可能希望将其放入 package.json 中的脚本中,以便每次都更轻松地运行/与团队中的其他人共享配置:

With this approach you may wish to put this into a script in your package.json to make it easier to run each time / share the config with others on your team:

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

<小时>

传统:

@angular/cli final 的更新:

angular-cli.json 中,您可以指定默认端口:

Inside angular-cli.json you can specify the port in the defaults:

"defaults": {
  "serve": {
    "port": 1337
  }
}

<小时>

旧版:

在 angular-cli@1.0.0-beta.22-1 中测试

angular-cli 中的服务器来自 ember-cli 项目.要配置服务器,请在项目根目录中创建一个 .ember-cli 文件.在那里添加您的 JSON 配置:

The server in angular-cli comes from the ember-cli project. To configure the server, create an .ember-cli file in the project root. Add your JSON config in there:

{
   "port": 1337
}

重新启动服务器,它将在该端口上提供服务.

Restart the server and it will serve on that port.

此处指定了更多选项:http://ember-cli.com/#runtime-configuration

{
  "skipGit" : true,
  "port" : 999,
  "host" : "0.1.0.1",
  "liveReload" : true,
  "environment" : "mock-development",
  "checkForUpdates" : false
}

这篇关于angular-cli 服务器 - 如何指定默认端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆