Image for post
Image for post

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?

Developer since the 80'th, working at Forschungszentrum Jülich ( …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store