javascript - 如何通过 Angular 4 中的 App Component 访问私有(private)属性?

标签 javascript jquery html css angularjs

我通过我在 app 组件中声明的代码获取当前 url 到我的 app.component.html

app.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  public href: string = "";

  constructor(private router: Router) {}

  ngOnInit() {
  }

}

app.component.html

<div class="page-wrap {{router.url}}">

  <app-header></app-header>

  <div class="content-wrap">

    <app-sidebar></app-sidebar>

    <div class="app-content">
      <flash-messages></flash-messages>
      <router-outlet></router-outlet>
    </div>

  </div>

</div>

当我通过“ng serve -prod”编译上面的代码时,系统提示我:

ERROR in C:/Users/theone/Documents/Node Projects SourceTree/dream-angular/src/$$_gendir/app/app.component.ngfactory.ts (58,68): Property 'router' is private and only accessible within class 'AppComponent'.

最佳答案

错误消息告诉您到底出了什么问题。该字段是私有(private)的。无法从模板访问它。尝试

constructor(public router: Router) { }

关于javascript - 如何通过 Angular 4 中的 App Component 访问私有(private)属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50083233/

相关文章:

javascript - FPDF 将图像放入 CELL 时出现奇怪的错误

javascript - 日期和时间范围选择器

javascript - JS 正则表达式 url 验证

javascript - 我的按钮没有改变功能

javascript - 使用 Angular ng-swipe-left 和 ng-swipe-right 在滑动过程中旋转图像

javascript - 在 HTML5 Canvas 上绘制图像

javascript - 如果标签属性类仅包含 jquery 中的该类,则删除标签

html5 : why is my progressbar represented in green when I ask it to be in red?

javascript - 如何在html5中使用css3在灯箱中显示编辑输入字段

javascript - 可以使用 jQuery 在两种尺寸之间切换/设置动画吗?