【发布时间】:2017-02-23 17:36:11
【问题描述】:
我目前正在尝试使用 jwt 创建登录功能,但是在尝试捕获活动路由的当前 url 时出现以下错误。
client?26cb:76 [默认] /Users/~/src/app/app.component.ts:51:75 属性“currentUrlTree”是私有的,只能在“Router”类中访问。
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) {
// logged in
this._zone.run(() => this._router.navigate(['stocklist']));
} else {
// 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();
}
}
【问题讨论】:
标签: angular typescript