我通过我在 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/