Angular 6无法修改host值?求替代参数方式的修改方案
Absolutely, this is a super common question after Angular CLI switched from .angular-cli.json to angular.json—lots of developers run into this when upgrading projects or setting up new ones!
The good news is you can still set the host directly in the config file, you just need to place it in the right section. Here's a step-by-step breakdown:
- Open your root project's
angular.jsonfile - Navigate to the
projectssection, then locate your project name (check thedefaultProjectfield at the top if you're unsure which project to target) - Drill into
architect > serve > options - Add a new line with
"host": "your-desired-host"— for example,"host": "0.0.0.0"to allow external device access, or a custom local domain like"host": "local.myapp.com"
Here’s a sample of what the updated config will look like:
{ "projects": { "my-angular-project": { "architect": { "serve": { "options": { "browserTarget": "my-angular-project:build", "port": 4200, "host": "0.0.0.0" // Your custom host value here }, "configurations": { "production": { "browserTarget": "my-angular-project:build:production" } } } } } } }
If you need environment-specific host settings (like a different host for staging vs production), you can also add the host property inside the corresponding configurations entry:
"configurations": { "production": { "browserTarget": "my-angular-project:build:production", "host": "prod.myapp.com" } }
Save the file, and running ng serve will automatically use your configured host—no more needing to pass it as a command-line argument!
内容的提问来源于stack exchange,提问作者Σωτήρης Ραφαήλ

