【问题标题】:Property 'currentUrlTree' is private and only accessible within class 'Router'属性“currentUrlTree”是私有的,只能在“Router”类中访问
【发布时间】: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


    【解决方案1】:

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

    这里是documentation。如您所见,它们不会公开“currentUrlTree”,因为它是私有的:

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2017-08-25
      • 2017-08-27
      • 2021-01-10
      • 2019-06-17
      • 2018-01-01
      • 2019-06-19
      相关资源
      最近更新 更多