angular-cli server - how to specify default port

前端 未结 8 990
星月不相逢
星月不相逢 2020-12-04 10:42

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?

相关标签:
8条回答
  • 2020-12-04 11:20

    The answer provided by elwyn is correct. But you should also update protractor config for e2e.

    In angular.json,

    "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "port": 9000,
            "browserTarget": "app:build"
          }
        }
    

    In e2e/protractor.conf.js

    exports.config = {
        allScriptsTimeout: 11000,
        specs: [
            './src/**/*.e2e-spec.ts'
        ],
        capabilities: {
            'browserName': 'chrome'
        },
        directConnect: true,
        baseUrl: 'http://localhost:9000/'
    }
    
    0 讨论(0)
  • 2020-12-04 11:22

    For @angular/cli v6.2.1

    The project configuration file angular.json is able to handle multiple projects (workspaces) which can be individually served.

    ng config projects.my-test-project.targets.serve.options.port 4201

    Where the my-test-project part is the project name what you set with the ng new command just like here:

    $ ng new my-test-project
    $ cd my-test-project
    $ ng config projects.my-test-project.targets.serve.options.port 4201
    $ ng serve
    ** Angular Live Development Server is listening on localhost:4201, open your browser on http://localhost:4201/ **
    

    Legacy:

    I usually use the ng set command to change the Angular CLI settings for project level.

    ng set defaults.serve.port=4201
    

    It changes change your .angular.cli.json and adds the port settings as it mentioned earlier.

    After this change you can use simply ng serve and it going to use the prefered port without the need of specifying it every time.

    0 讨论(0)
提交回复
热议问题