Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a string constant in angular 4?

Tags:

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 ?

like image 683
aish Avatar asked May 11 '17 13:05

aish


People also ask

How do you declare a string constant?

To define a string constant in C++, you have to include the string header library, then create the string constant using this class and the const keyword.

Can a string be a constant?

A string constant is an arbitrary sequence of characters that are enclosed in single quotation marks (' '). For example, 'This is a string'. You can embed single quotation marks in strings by typing two adjacent single quotation marks.

How do I create a constant in TypeScript?

Typescript constants are variables, whose values cannot be modified. We declare them using the keyword const . They are block-scoped just like the let keyword. Their value cannot be changed neither they can be redeclared.

What is the symbol of string constant?

In BASIC source code, character string constants are a sequence of ASCII characters enclosed in single or double quotation marks, or backslashes ( \ ).


1 Answers

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;
      }
    }
like image 171
Cory Avatar answered Oct 24 '22 19:10

Cory