Hello Devs,

In this tutorial, we are going to learn how to define constants in angular 10.

We will create GlobalConstants file for adding global variables in our application. In that file i added two variables as apiURL and siteTitle.


src/app/common/global-constants.ts

export class GlobalConstants {

    public static apiURL: string = "https://rathorji.in/";

      

    public static siteTitle: string = "This is example of Rathorji.in";

}

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);

    }

}


Output:

https://rathorji.in/
This is example of Rathorji.in
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
Object
Live Reloading enabled.


I hope this example helps you.