It is a good practice to make a separate file for your constants. Where, there could be multiple scenarios, in which I prefer/ recommend the Second one from the followings -
1) Export every constant & import as needed; if you don't have too many constants.
export const TEST = "testval";
export const TEST2 = "testval2";
Import as -
import { TEST, TEST2 } from './app.constants';
2) Create & export a single Injectable class if you have too many constants & want a hassle-free import.
So, your app.constants.ts would be -
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AppConstants {
public TEST = "testval";
public TEST2 = "testval2";
}
Then, you could simply inject it in your required class like -
constructor(private constants: AppConstants)
& use as - constants.TEST
3) You could also export an Object as -
export const constObj = {
TEST: "testval",
TEST2: "testval2"
};
And import it as -
import { constObj } from './app.constants';
& use as - constObj.TEST