属性'currentUrlTree'是私有的,只能在'Router'类中访问

时间:2016-10-14 10:11:24

标签: angular typescript

我目前正在尝试使用jwt创建一个登录函数,但是当我尝试捕获活动路径的当前URL时,我当前收到以下错误。

客户端?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();
}
}

1 个答案:

答案 0 :(得分:0)

正如它所说,currentUrlTree是一个私人成员。使用其他成员访问当前URL,例如:Router.url。

这是documentation。正如你所看到的那样,他们不公开'currentUrlTree',因为它是私有的: