简体   繁体   中英

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:

<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 . Thank you for reading.

All the properties defined in the class that you use in the html template should be public . You have defined the authService as private and it's complaining about this.

In order to fix the issue you have to make it public:

constructor(public authService: AuthService) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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