简体   繁体   中英

How to create Global variable in Angular for multiple Components

I wanted to create a global variable which is of type boolean, I wanted to utilize the same variable in multiple components ( Without using Service ).To be precise I also wanted to capture the variable value from any component if it is changed in other components.

try declaring the variable in environment.ts file

There are few options to do it.

  1. Environment variables: Declare the variable in environment.ts . Do note that you need to replicate it across envionment.prod.ts and other environment files if you may have.
  2. Injection Token: Create a custom injection token, and pass it as a provider in your Component or Module. Details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM