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 ?
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.
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.
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.
In BASIC source code, character string constants are a sequence of ASCII characters enclosed in single or double quotation marks, or backslashes ( \ ).
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With