Matthew Flynn
Matthew Flynn

Reputation: 3931

using --environments with Angular SPA on Asp.net core 2 Visual Studio 2017

I am running the Visual Studio 2017 Angular SPA template and I want to add in the use of angular environment variables to work in production and development.

I am running the following version of angular in my package.json file;

    "@angular/animations": "^4.4.7",
    "@angular/cli": "^6.0.8",
    "@angular/common": "^4.4.7",
    "@angular/compiler": "^4.4.7",
    "@angular/compiler-cli": "^4.4.7",
    "@angular/core": "^4.4.7",
    "@angular/forms": "^4.4.7",
    "@angular/http": "^4.4.7",
    "@angular/platform-browser": "^4.4.7",
    "@angular/platform-browser-dynamic": "^4.4.7",
    "@angular/platform-server": "^4.4.7",
    "@angular/router": "^4.4.7",

I have created the /environments folder in my ClientApp/app foldere and added my environment.prod.ts and environment.ts files.

An example is;

export const environment = {
    production: false,
    appUrl: 'http://localhost:61996',
    apiUrl: 'http://localhost:61996'
}; 

How do I then go about configuring this to work with my SPA? As the current template doesn't contain a .angular-cli.json file I see referenced on tutorial websites?

My aim is to be able to run in debug using 'environment.prod.tsand then web deploy theenvironment.ts` with my publish.

Upvotes: 1

Views: 1302

Answers (1)

take
take

Reputation: 2222

In your angular.json you have an configuration section where you can define a fileReplacement.

"build": {
    "configurations": {
         "dev": {
              "fileReplacements": [ {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.dev.ts"
                }]
           },
           "prod": {
              "fileReplacements": [ {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                }]
           }
     }
}

To use the dev environment you have to add --configuration=dev

Upvotes: 1

Related Questions