How to get base url in angular 5?

前端 未结 9 951
抹茶落季
抹茶落季 2020-12-06 04:01

My current url is http://localhost:4200/test/dashboard.

I want to print base url i.e http://localhost:4200 using angular 5 features.

9条回答
  •  无人及你
    2020-12-06 04:28

    I have used location from Rotemya's answer like this

    import { Location } from '@angular/common';
    
    constructor(public location: Location) { }
    

    But this.location.origin didn't work for me. So I have used this.location.path.name

     ngOnInit() {
            console.log(this.location.path.name);
      }
    

提交回复
热议问题