1

我真的不知道还有什么问题。

服务代码:

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

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

组件代码:

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

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

在 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>

我对 HTML 代码的问题是authService. 感谢您的阅读。

4

1 回答 1

1

您在 html 模板中使用的类中定义的所有属性都应该是public。您已将 authService 定义为私有的,并且它对此有所抱怨。

为了解决这个问题,您必须将其公开:

constructor(public authService: AuthService) { }
于 2018-07-21T08:02:37.567 回答