Angular N - what is the best practice of declaring a constant file?
I will do it like this:
export const MAXIMUM_NUMBER = 10;
and
import { MAXIMUM_NUMBER } from './emails.constants';
So only import what you use, not everything.
but if you still want to use everything, you can do it similarly as you have done, just change it a bit:
import * as EmailConstants from './emails.constants';
Then you can still use
EmailConstants.MAXIMUM_NUMBER
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