app.component.ts
import { Location } from '@angular/common'; import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { constructor(private location: Location) { } ngOnInit() { //如果访问路径是http://localhost:4200/#/home,输出结果是 /home console.log(this.location.path()); } }
访问http://localhost:4200/#/home输出结果如下图: