I want to declare variable in my app.module and access it from children components
without declare it again or use global injection.
Is this possible?
You can define a String Token and inject it at your component via Injector from @angular/core.
App.module.ts
const testVar = 'some value';
@NgModule({
  ...
  providers: [
    { provide: 'A', useValue: testVar}   // define a string token
  ]
})
export class AppModule { }
Component
import { Injector } from '@angular/core';
constructor(private injector: Injector) {
  const A = this.injector.get('A');
  console.log(A);
}
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With