简体   繁体   English

标识符“ authService”是指组件的私有成员

[英]Identifier 'authService' refers to a private member of the component

I really do not know where else is the problem. 我真的不知道问题出在哪里。

Service codes: 服务代码:

import { Injectable } from '@angular/core';

@Injectable()
export class AuthService {
  logout() {
    localStorage.removeItem('token');
  }
}

Component codes: 组件代码:

import { AuthService } from './../services/auth.service';

export class HomeComponent {
  constructor(private authService: AuthService) {}
}

And at the end of the HTML codes: 在HTML代码的末尾:

<h1>
  Home Page
</h1>
<p *ngIf="authService.isLoggedIn">
  Welcome {{ authService.currentUser.name }}
</p>
<ul>
  <li *ngIf="authService.isLoggedIn() && authService.currentUser.admin">
    <a routerLink="/admin">Admin</a>
  </li>
  <li *ngIf="authService.isLoggedIn()">
    <a routerLink="/login">Login</a>
  </li>
  <li *ngIf="authService.isLoggedIn()" (click)="authService.logout()">
    <a>Logout</a>
  </li>
</ul>

My problem with the HTML code is the authService . HTML代码的问题是authService Thank you for reading. 感谢您的阅读。

All the properties defined in the class that you use in the html template should be public . 在html模板中使用的类中定义的所有属性都应为public You have defined the authService as private and it's complaining about this. 您已将authService定义为私有的,并且对此有所抱怨。

In order to fix the issue you have to make it public: 为了解决此问题,您必须将其公开:

constructor(public authService: AuthService) { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM