Angular Global Variable – How to Declare Global Variable in Angular 9/8?

angular global variable or Declare Global Variable in Angular 9/8 Example.

Angular Global Variable

src/app/common/global-constants.ts

export class GlobalConstants {
    public static apiURL: string = "https://www.itsolutionstuck.com/";
      
    public static siteTitle: string = "Web Development Tutorials";
}

src/app/app.component.ts

import { Component, OnInit } from '@angular/core';
import{ GlobalConstants } from './common/global-constants';
   
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
    title = GlobalConstants.siteTitle;
  
    constructor() { 
        console.log(GlobalConstants.apiURL);
    }
   
    ngOnInit() {
        console.log(this.title);
    }
}

Results:

https://www.itsolutionstuck.com/

Web Development Tutorials

Angular is running in the development mode means dev platform. Call enableProdMode() to enable the Live mode.

Object

Live Reloading enabled.

Leave a Reply

Your email address will not be published.