
globals in Angular 2?
Developing an Angular 2 application I recently needed a variable in different contexts of my application.
Knowing Angular 1.x I thought about a value provider at first place, but then I imagine, that using a global variable should also do the job. And this should be easily done by creating a separate file exporting this variable.
export let adviceId = undefined;
Okay, so let`s use it:
import {adviceId} from '../globals';
adviceId = 42;
But the compiler denies. It is not possible to change an exported variable:
Invalid left-hand side of assignment expression.
My next guess was to put it into an object:
export let globals = {
adviceId: undefined
};
And this actually works:
import {globals} from '../globals';
globals.adviceId = 42;
But is this a good idea? It is definitly not, because Angular2 has providers for that ;-)
Do you want to read about it?