0

In spring boot, the base url maybe localhost:8080 or a different one. Now I want to use it in angular. I saw that many examples online just hardcode it in angular http client call code.

But the base url can be changed, for example the port number changed or the environment is changed from dev to qa or production. So my question is that possible dynamically expose it from spring boot and retrieve it from angular?

1
  • 3
    you can use environment files in angular to set different url. Then you can build the project using one of these environments files so it matchs aq/dev/prod url. You can't dynamically expose it from spring boot in your situation because in order to retrieve it from Angular, you need to know the actual url...that you don't yet know Commented Jan 5, 2021 at 13:39

1 Answer 1

1

Your app should contain files at src/environments called environment.ts and environment.prod.ts. You can include a constant key value pair for your apiURL in this file like so:

export const environment = {
  production: false,
  apiUrl: 'localhost:8080'
}

And also include your production URL in the prod file. You can then use it in a file like so:

import { environment } from 'src/environment/environment.ts';
...
export class AppComponent implements OnInit {

  url: string;

  ngOnInit(): void {
     this.url = environment.apiUrl;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.