Issue
I am basically trying to set the API URL path in appSettings so I can change back and forth from prod and dev environments easily.
This is my appSettings.json file. apiURL is the variable I am trying to set and get.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"apiURL": "http://10.241.2.68:8132/api"
}
Here is my page that is trying to access the environment variable that I set in appSettings.json.
import { Injectable } from '@angular/core';
// import { Defendant } from 'src/app/Models/Defendant';
import { Charge } from 'src/app/Models/Charge';
import { DefendantItem } from '../Models/DefendantItem';
import { Defendant_Charge } from '../Models/Defendant_Charge';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { AppConfiguration } from "read-appsettings-json";
@Injectable({
providedIn: 'root'
})
export class ChargeService {
//console.log(AppConfiguration.Setting().apiURL);
////////////////////////////////////////////////////////////////////
// below is where I am trying to reference the environment variable
// the entire program bombs and displays "GET \" on the page.
ChargeControllerUrl = AppConfiguration.Setting().apiURL + "/Charges";
//ChargeControllerUrl = "http://10.241.2.68:8132/api/Charges";
}
Solution
As stated in comment, appsettings.json is not for angular application. here you can go through this tutorial to find necessary steps for using enviornment. here are some quick steps:
import { environment } from './../environments/environment';
...
const baseUrl: string = environment.baseUrl;
where environment.ts may look like
export const environment = {
production: false,
baseUrl: "http://localhost:45644/api/",
};
live example here
Answered By - Arham Anees
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.