如何解决属性“ authenticationService”是私有的,只能在“ AdminComponent”类中访问
我正在尝试部署一个有角度的项目,但我不断从此类中得到这个错误。
<div class="page-content">
<app-breadcrumb [breadcrumbs]="breadcrumbs"></app-breadcrumb>
<mat-toolbar class="toolbar-header">
<mat-toolbar-row>
<span class="font-larger">Welcome {{authenticationService.getName()}}!</span>
<span class="mat-toolbar-spacer"></span>
</mat-toolbar-row>
</mat-toolbar>
<mat-grid-list cols="3" rowHeight="240px" class="app-admin-tiles">
<mat-grid-tile *ngFor="let element of tools"
[colspan]="element.cols"
[rowspan]="element.rows"
>
<app-admin-tile
[color]="element.color"
[text]="element.text"
[icon]="element.icon"
[url]="element.url"
style="width: 100%;"
></app-admin-tile>
</mat-grid-tile>
</mat-grid-list>
</div>
解决方法
请为变量分配authenticationService.getName()
并在模板中使用它。
在ts中,例如:
ngOnInit() {
this.name = authenticationService.getName();
}
在模板使用中,例如<span class="font-larger">Welcome {{name }}!</span>
根据我的评论here展开,将模板表达式中的函数绑定到数据或使用默认更改检测策略进行数据绑定不是一个好主意。而是在组件控制器中调用该函数,并将其结果绑定到模板中。
尝试以下
控制器
export class AppComponent implements OnInit {
name: string;
constructor(private authenticationService: AuthenticationService) { }
ngOnInit() {
this.name = this.authenticationService.getName(); // <-- get name here
}
}
模板
<!-- Show only if the `name` is initialized with a valid value -->
<span *ngIf="name" class="font-larger">Welcome {{ name }}!</span>
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。