Global Constants

Hey guys,

where would you save global constants, that needs to be used from every point in the application? I would not like to put those values inside the environment.prod.ts file… Any other idea?

Thanks,
Oliver

Hi
Are you using Angular? If so, then I’d use a provider for that - if the constants you are referring to are not environment related.
Tom

1 Like

Got an example for a provider of constant values?

Are you using angular?

This is what I would do:
https://angular.io/guide/providers

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class UserService {

 giveMyStaticData(paramName:string) {
          // based on hashtable return the data in cloned way ...

 }
}

And have a getter method giving the data to whoever needs it - ideally via Observable pattern or as a clone to avoid other parts changing content of objects - an unexpected side-effect of working with javascript.

But I would really just like to export variables (as constants) - would a provider not be a little bit of overhead?

Fair point… of course also pending where you get your constants from if retrieved dynamically…

Alternatives:

I reckon you know that constants strictly spoken don’t exists in javascript - no runtime checks on code changing the content.