Joss MT
Joss MT

Reputation: 299

Typescript - store static global variables and retrieve by key?

I am trying to do the following. Store variables in a static global file, say with:

 declare const MYVAR = 'Some unchanging data';

Then later be able to retrieve the information knowing only the key 'MYVAR', i.e.

globalFile.findValueByKey('MYVAR');

I know typescript doesn't really use reflection - so what is the best way to go about handling this?

Upvotes: 0

Views: 1074

Answers (1)

Jazib
Jazib

Reputation: 1381

Create a global provider class and use it where ever you want

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


@Injectable()
export class GlobalProvider {

 public MYVAR:string = 'Some unchanging data';
 constructor() {
 }

and you can import it like any other class and use

import { GlobalProvider } from "providers/global";

someClass{
constructor(private gp: GlobalProvider){
 console.log(gp.MYVAR);
 }

}

Upvotes: 1

Related Questions