0

I would like to know if you can configure the base href as an environment variable of a tomcat or application server.

for example:

index.html

<base href="/${environment.tomcat}/">

or an environment variable for the operating system?

4
  • Possible duplicate of Angular 2 base href from environment variable Commented May 11, 2019 at 13:10
  • I refer to environment variables of an application server, not of ng build --prod --base-href Commented May 11, 2019 at 13:24
  • @Dani check the solution Commented May 12, 2019 at 6:23
  • I answered a similar problem here stackoverflow.com/a/67091452/1843984 Commented Apr 14, 2021 at 12:15

1 Answer 1

2

environment.ts

export const environment = {
  production: false,
  tomcat: '/'
};

environment.prod.ts

export const environment = {
  production: true,
  tomcat: '/my-app/'
};

And in your app module:~

import { Component, NgModule } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { environment } from '../environments/environment';

@NgModule({
  providers: [{provide: APP_BASE_HREF, useValue: environment.tomcat}]
})
class AppModule {}

OR

Using Angular CLI built-in feature:~

# Sets base tag href to /myUrl/ in your index.html
ng build --base-href /myUrl/
ng build -bh /myUrl/

you can update scripts in package.json:~

"scripts": {
  //...
  "start": "ng serve -bh /anotherUrl/",
  "build": "ng build -bh /tomcatServerURL/",
  //..
},

Hope this is helpful!

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.