How to make a string constant in angular 4?

angular

Solution

I'm not sure if i understand your question but if you want to create constants, you can do it in a different class and import it.

constants.ts

export class Constants {
  public static get HOME_URL(): string { return "sample/url/"; };
}

sample.component.ts

   import { Constants } from "./constants";
    @Component({

    })
     export class SampleComponent {
      constructor() {
       let url = Constants.HOME_URL;
      }
    }

Problem

In my service, I am using a `http` post. I want to set the URL as a constant. ``` return this.http.get(this.config.API_URL+'users', options).map(res=>res.json()); ``` I tried with a service: ``` import {Injectable} from '@angular/core'; @Injectable() export class AppService { API_URL :String; constructor() { this.API_URL = 'some url'; } } ``` Is there any other method to make a constant value in Angular4 ?

Original source

Related problems