Programing

Angular 6에서 'ng serve'를 통해 환경을 설정하는 방법

crosscheck 2020. 7. 3. 19:46
반응형

Angular 6에서 'ng serve'를 통해 환경을 설정하는 방법


Angular 5.2 앱을 Angular 6으로 업데이트하려고합니다. Angular 업데이트 가이드 ( angular-cliv6 업데이트 포함)의 지침을 성공적으로 따랐 으며 이제는

ng serve --env=local

그러나 이것은 나에게 오류를 준다 :

알 수없는 옵션 : '--env'

여러 환경 ( dev/local/prod)을 사용하는데 이것이 Angular 5.2에서 작동하는 방식입니다. Angular 6에서 환경을 설정하는 방법은 무엇입니까?


당신은 새로운 사용해야 configuration옵션 (이 작품 ng buildng serve뿐만 아니라를)

ng serve --configuration=local

또는

ng serve -c local

angular.json파일 을 보면 각 구성 (Aot, Optimizer, 환경 파일 등)에 대한 설정을보다 세밀하게 제어 할 수 있습니다.

"configurations": {
  "production": {
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true,
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ]
  }
}

환경 별 구성 관리에 대한 자세한 정보는 여기 에서 얻을 수 있습니다 .

아래의 다른 응답에서 지적했듯이 새 '환경'을 추가해야하는 경우 빌드 작업 및 필요에 따라 서브테스트 작업에 새 구성을 추가해야합니다 .

새로운 환경 추가

편집 : 명확하게하려면 파일 교체를 build섹션 에서 지정해야합니다 . 따라서 ng serve특정 environment파일 ( dev2 ) 과 함께 사용 하려면 먼저 build새로운 dev2 구성 을 추가하기 위해 섹션을 수정해야 합니다

"build": {
   "configurations": {
        "dev2": {

          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.dev2.ts"
            }
            /* You can add all other options here, such as aot, optimization, ... */
          ],
          "serviceWorker": true
        },

그런 다음 방금 선언 servedev2 build 구성을 가리키고 섹션을 수정하여 새 구성을 추가하십시오.

"serve":
      "configurations": {
        "dev2": {
          "browserTarget": "projectName:build:dev2"
        }

그런 다음 ng serve -c dev2dev2 구성 파일을 사용하는을 사용할 수 있습니다


This answer seems good.
however, it lead me towards an error as it resulted with
Configuration 'xyz' could not be found in project ...
error in build.
It is requierd not only to updated build configurations, but also serve ones.

So just to leave no confusions:

  1. --env is not supported in angular 6
  2. --env got changed into --configuration || -c (and is now more powerful)
  3. to manage various envs, in addition to adding new environment file, it is now required to do some changes in angular.json file:
    • add new configuration in the build { ... "build": "configurations": ... property
    • new build configuration may contain only fileReplacements part, (but more options are available)
    • add new configuration in the serve { ... "serve": "configurations": ... property
    • new serve configuration shall contain of browserTarget="your-project-name:build:staging"

You can try: ng serve --configuration=dev/prod

To build use: ng build --prod --configuration=dev

Hope you are using a different kind of environment.


For Angular 2 - 5 refer the article Multiple Environment in angular

For Angular 6 use ng serve --configuration=dev

Note: Refer the same article for angular 6 as well. But wherever you find --env instead use --configuration. That's works well for angular 6.


Use this command for Angular 6 to build

ng build --prod --configuration=dev

You can use command ng serve -c dev for development environment ng serve -c prod for production environment

while building also same applies. You can use ng build -c dev for dev build


Angular no longer supports --env instead you have to use

ng serve -c dev

for development environment and,

ng serve -c prod 

for production.

NOTE: -c or --configuration

참고URL : https://stackoverflow.com/questions/50174584/how-to-set-environment-via-ng-serve-in-angular-6

반응형