简体   繁体   中英

Get application url using angular 6

I have an angular6 application and I am using the same code in different domains. Few of them are in the root of the domain and some are uploaded in folders and subfolders.

example :

  example1.com
  example2.com/myapp
  example3.com/myapp/v1

For some additional purposes, I need the base URL of the application. For this, I have tried location.origin , but the problem is, It returns only the domain part if we are using subfolders.

Currently, I am using the below code for getting the URL in each page.

this.base_url = location.href.split('order_confirmation')[0]; 

This will work if the current route is start with "order_confirmation", But if the base URL needed for another page, I need to explore it with the route string and get the URL from that array. Is there any better method available in angular 6 for getting the URL directly?

I got this working with the following lines of code,

import {PlatformLocation } from '@angular/common';
export class ConfirmationComponent implements OnInit {
  public base_url;
   constructor(private platformlocation: PlatformLocation){
    this.base_url = location.origin + this.platformlocation.getBaseHrefFromDOM();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM