Sandip Nag
Sandip Nag

Reputation: 998

Global file access from anywhere in angular 4

In my angular 4 project I have a made a file of some constants and getting access to it I am importing that file to my services file and can access the variables, but the problem is in each and every services I have to import that file.

Is there any process so that I can load my constant file in one place and can use that file variables throughout the module not importing it each and every time.

Thank you.

Upvotes: 2

Views: 113

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222722

Simple answer is NO. But you can export as a single variable and access it as,

You can do something like this:

In your config.ts:

export const Keys = {
    CONFIG_1: 'VALUE',
    CONFIG_2: 'VALUE'
};

Then in the file that you importing it you can do below:

import { Keys } from './config';
Keys.CONFIG_1 //return the value 'value'

You should always import the file. But if you still wanna do that you can do it in the ugliest way like this:

window.globalvar = "myapi"

Upvotes: 1

Related Questions