How to make a string constant in angular 4?

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

You can simply export a constant using es6/typescript modules if that's all you need:

constants.ts:

export const API_URL: string = 'api/url';

And import where needed:

import { API_URL } from './constants.ts';

...

return this.http.get(API_URL+'users', options)
                .map(res=>res.json());

or if you have many constants you can import them all:

import * as constants from './constants.ts';

...

return this.http.get(constants.API_URL+'users', options)
                    .map(res=>res.json());

If you want to make your constants configurable per application on startup, you can use providers. Check out the top answer in this link: how do I get angular2 dependency injection to work with value providers

Tags:

Angular