0

While building my angular application using ng build, I have provided the --base-href flag for my application as foo. So the URL for my application looks like https://10.10.10.19:8080/foo. However I want to navigate to a page that has bar instead in the URL, i.e. https://10.10.10.19:8080/bar.

I tried router.navigateByUrl('/bar') but it creates a URL https://10.10.10.19:8080/foo/bar which is not what I am looking for.

Can someone help me with what is missing or going wrong here?

Naman Arora
  • 93
  • 3
  • 12
  • show me your code – ram12393 Dec 05 '18 at 04:38
  • Instead to be specific, show us your routing files. And also try changing the baseHref = "/" in index.html – KiraAG Dec 05 '18 at 05:32
  • setting up base for your app is explained in an article that i have published , i hope this will help you : http://joeljoseph.net/angular-6-deploy-on-apache-server-by-solving-404-not-found-error-on-page-refresh/ – Joel Joseph Dec 05 '18 at 05:41

3 Answers3

0

Not sure after build but during development i use following code and its works for me..

this.router.navigate(['../bar'], { relativeTo: this.route });
R. Viral
  • 396
  • 1
  • 6
  • What is this.route here? Is it the ActivatedRoute instance you're talking about? – Naman Arora Dec 05 '18 at 05:29
  • I am presently at the URL location https://10.10.10.19:8080/foo/a1 and want to navigate to https://10.10.10.19:8080/bar/b2. With what you suggested I tried the same with `this.router.navigate(['../../bar/b1'], { relativeTo: this.route });` But the same fails with invalid number of '../'. Any thoughts on this? – Naman Arora Dec 05 '18 at 05:42
0

Basically you want your base href to be set dynamically as you can not remove only base href (AFAIK).

Import base_HREF on main.ts

import { APP_BASE_HREF } from '@angular/common';

and set your providers as below

providers: [
    {
        provide: APP_BASE_HREF,
        useValue: '/' + (window.location.pathname.split('/')[1] || '')
    }
]

credit to this link : this link can be of use: Angular 2 / 4 / 5 - Set base href dynamically

T. Shashwat
  • 1,065
  • 8
  • 23
-1

It can be achieved through dependency injection or through httpInterceptors.

I hope the below link will help your question. http://www.projectcodify.com/angular-set-base-url-dynamically