简体   繁体   English

如何在 Angular 中为多个组件创建全局变量

[英]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.我想创建一个类型为 boolean 的全局变量,我想在多个组件中使用相同的变量(不使用服务)。确切地说,如果它在其他组件中发生更改,我还想从任何组件中捕获变量值.

try declaring the variable in environment.ts file尝试在 environment.ts 文件中声明变量

There are few options to do it.有几个选项可以做到这一点。

  1. Environment variables: Declare the variable in environment.ts .环境变量:在environment.ts中声明变量。 Do note that you need to replicate it across envionment.prod.ts and other environment files if you may have.请注意,如果可能的话,您需要在envionment.prod.ts和其他环境文件中复制它。
  2. Injection Token: Create a custom injection token, and pass it as a provider in your Component or Module.注入令牌:创建自定义注入令牌,并将其作为提供者传递到您的组件或模块中。 Details细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为所有组件创建全局预加载器。 角度2 - How to create global preloader to all components. Angular 2 如何使全局变量和函数可以在角度4的所有组件中访问 - how to make global variable and functions which can be accessible in all the components in angular 4 react函数组件中如何实现全局变量 - How to implement global variable in react functional components Angular 5 全局组件及其服务 - Angular 5 global components and their services 如何在Angular 4中创建嵌套组件? - How to create nested components in Angular 4? Angular - 如何创建全局函数? - Angular - How to create global functions? Angular 使用 ComponentFactoryResolver 创建多个组件 - Angular Create multiple components using ComponentFactoryResolver 如何使Dragula成为Angular 2中的全球提供者以在组件之间共享 - How to make Dragula a global provider in Angular 2 to share across components 如何共享变量并跟踪角度v4 ts中多个组件的变化 - How to share a variable and track it's change in multiple components in angular v4 ts 如何使用全局变量在 astro 页面中动态渲染 vuejs 组件? - How to dynamically render vuejs components in an astro page using a global variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM