Meine aktuelle URL ist http: // localhost: 4200/test/dashboard .
Ich möchte die Basis-URL, d. H. Http: // localhost: 4200 , mit den Funktionen von "angle 5" drucken.
console.log (Speicherort);
console.log (location.href);
um eine Basis-URL zu erhalten: console.log(location.Origin);
Keine winkelspezifischen Funktionen erforderlich, window.location.Origin
erledigt das für Sie.
PlatformLocation enthält weitere Details zur URL:
import {PlatformLocation } from '@angular/common';
constructor(platformLocation: PlatformLocation) {
console.log((platformLocation as any).location);
console.log((platformLocation as any).location.href);
console.log((platformLocation as any).location.Origin);
}
Das funktioniert bei mir nicht (Angular 7):
this.location.path.name
Aber ich fand, dass es möglich ist, es aus dem Dokument zu bekommen:
import { Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
constructor(@Inject(DOCUMENT) private document: Document) {
const Origin = this.document.location.Origin;
}
Sie können es versuchen (kann alle Details des aktuellen Standorts abrufen)
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
@Component({
selector: 'some-component',
templateUrl: './component.html',
styleUrls: ['./component.scss']
})
export class SomeComponent implements OnInit {
constructor(location: Location) {}
ngOnInit() {
console.log(this.location._platformStrategy._platformLocation.location);
}
}
Ich habe Standort von Rotemya 's Antwort so verwendet
import { Location } from '@angular/common';
constructor(public location: Location) { }
Aber dieser Ort hat bei mir nicht funktioniert. Also habe ich this.location.path.name verwendet
ngOnInit() {
console.log(this.location.path.name);
}
Sie können 'Location' aus dem 'common' -Paket importieren:
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common'; // <--- Here
import { Router } from '@angular/router';
@Component({
selector: 'some-component',
templateUrl: './component.html',
styleUrls: ['./component.scss']
})
export class SomeComponent implements OnInit {
constructor(location: Location) {}
ngOnInit() {
console.log(this.location.Origin); // <--- Use Here
}
}