xxxxxxxxxx
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
template: 'The href is: {{href}}'
/*
Other component settings
*/
})
export class Component {
public href: string = "";
constructor(private router: Router) {}
ngOnInit() {
this.href = this.router.url;
console.log(this.router.url);
}
}
xxxxxxxxxx
import { Component } from '@angular/core';
import { Location } from '@angular/common';
@Component({
selector: 'app-example',
template: '<p>Current URL: {{ currentUrl }}</p>',
})
export class ExampleComponent {
currentUrl: string;
constructor(private location: Location) {
this.currentUrl = this.location.path();
}
}