2016-10-14 52 views
0

我目前正在嘗試使用jwt創建登錄函數,但是當前嘗試捕獲活動路由的當前url時,我正在獲取以下錯誤。屬性'currentUrlTree'是私有的,只能在類'路由器'內訪問

client?26cb:76 [default] /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(); 
} 
} 

回答

0

,因爲它說,currentUrlTree是一個私有成員。使用另一個成員來訪問當前的URL,如:Router.url。

這是documentation。正如你可以看到他們不公開'currentUrlTree',因爲它是私密的:

相關問題