Global variables


#1

I am trying to define global variables that are accessible from, and modifiable by any Component or Injectable. I do not want to create this as a separate class and have it imported to places where it’s being used. Rather, I would like it to be accessible without it being a property of a class or linked to a particular namespace. I’ve experimented with placing these variables within the declarations.d.ts file, and although the editor is able to detect it properly, during run time it produces Error: Undefined. My declarations.d.ts file looks something like this:

declare module '*';
declare module 'global' {
  const CLIENT_ID: string = 'ABCDEF';
  const CLIENT_SECRET: string = 'ABC123';
  const SERVER_URL: string = 'https://fjekjqekewqjkwqj.tk';

  var ACCESS_TOKEN: string;
  var SERIES_ID: string;

  interface LatLngLiteral {
    lat: number;
    lng: number;
  }

  interface LatLngBoundsLiteral {
    east: number;
    north: number;
    south: number;
    west: number;
  }
}

#2

I think typically this sort of thing is done with singleton services. Why are you fixated on avoiding that style?


#3

I guess it makes sense now that I think of it to have the variables exported from a file, and imported into wherever it is needed. My initial goal was to be able to define certain constants and interfaces that were to be available everywhere without the need of explicitly importing them every time, as well as certain global variables that could be modified my on component, then read by another.