Angular: Declaring target domain/IP globally

  angular

I am working on an application and sending an HTTP request to the backend server. Since, there are a lot of components therefore, each time I want to deploy the application I have to change the localhost to the target domain/IP.

this.http.post('http://localhost:3000/api/login', form.value, {responseType: "text"})
  .subscribe(responseData => {
     console.log(responseData);
})

Before building it for production mode I have to do something like this for each HTTP request:

this.http.post('http://IP.OF.THE.SERVER:3000/api/login', form.value, {responseType: "text"})
  .subscribe(responseData => {
     console.log(responseData);
})

or, something like this for each separate component

this.http.post(`http://${this.ipServer}:3000/api/login`, form.value, {responseType: "text"})
  .subscribe(responseData => {
     console.log(responseData);
})

My question is that is there any way I can declare the domain/IP globally somewhere in the project like in angular.json file so, that it can be used by every angular component?

I don’t want to use service for this purpose.

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.