Angular 10 检查用户是否为空以隐藏组件元素

Angular 10 check if a user is null to hide component element

我有一个组件,如果用户为 null,我想在其中隐藏一个元素。目前它显示用户是否存在:

 <ng-container *ngIf="user | async as _user">

这是component.ts部分:

 this.user = this.auth.userSubject.asObservable();

我想做相反的事情:

 <ng-container *ngIf="user | async as _user">

以便它仅在 user$ 为 null 时显示

在注销组件的顶部是:

 userSubject: BehaviorSubject<UserModel>;

当他们注销时:

this.userSubject.next(null);

如果你想要相反的行为,就否定你的*ngIf

<ng-container *ngIf="!(user | async)">

简单示例:StackBlitz

应该适合您