Currently, I am in the process of developing a login feature using jwt. However, I am encountering an error when attempting to retrieve the current URL of the active route as shown below.
Error Message: [default] /Users/~/src/app/app.component.ts:51:75 Property 'currentUrlTree' is private and can only be accessed within the 'Router' class.
Source Code: app.component.ts
import {Component, NgZone, OnInit} from '@angular/core';
import {Router} from "@angular/router";
import {AuthService} from "./auth.service";
import {tokenNotExpired} from "angular2-jwt";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor (
private _router: Router,
private _authService: AuthService,
private _zone: NgZone
) {
}
ngOnInit():void {
this._router.navigate(['signin']);
this._authService.getLoggedInEvent().subscribe((val) => {
if (val) {
// User logged in
this._zone.run(() => this._router.navigate(['stocklist']));
} else {
// User logged out
this._zone.run(() => this._router.navigate(['signin']));
}
});
}
routeIsActive(routePath: string) {
let currentRoute = this._router.currentUrlTree.firstChild(this._router.currentUrlTree.root);
let segment = currentRoute == null ? '/' : currentRoute.segment;
return segment == routePath;
}
logout() {
this._authService.logout();
}
loggedIn() {
return tokenNotExpired();
}
}