hier ist das Problem:Angular 4: Bedingte Anzeige auf Benutzerstatus
Ich versuche, dieses kleine Tutorial zu folgen, um eine bedingte Anzeige in meiner aktuellen App zu machen, je nach Benutzer LoggedIn Status. Hier ist der Link:
https://loiane.com/2017/08/angular-hide-navbar-login-page/
Es sieht ziemlich einfach & effizient. Aber ich kann es nicht in meiner aktuellen Anwendung arbeiten. Im Anschluss an die Haupt-Tutorial Richtlinien, komme ich zu diesem Code:
Top-menu.component.ts:
authStatus: Observable<boolean>;
constructor(private authService: AuthService){
}
ngOnInit(){
this.authStatus = this.authService.isLoggedIn();
}
login(){
this.authService.login();
}
logout(){
this.authService.logout();
}
Top-menu.component.html:
<li><span (click)="login()">LOGIN</span></li>
<li><span (click)="logout()">LOGOUT</span></li>
//...
<li *ngIf="authStatus | async"><a [routerLink]="['/login']">CONDITIONAL LOGIN</a></li>
<li *ngIf="!authStatus | async"><a [routerLink]="['/logout']">CONDITIONAL LOGOUT</a></li>
auth.service.ts:
public loggedIn = new BehaviorSubject<boolean>(false);
get isLoggedIn(){
return this.loggedIn.asObservable();
}
login(){
this.loggedIn.next(true);
}
logout(){
this.loggedIn.next(false);
}
Kann mir jemand erklären, was ich falsch mache ...? Ich habe einige andere Beispiele im Internet, aber ohne Erfolg ...
Dank für das Lesen/help
wieso hast du 2 mal this.authStatus in contructor, oninit, was ist der Zweck –
rate ich falsch mit dem "Abonnement", das war nicht Teil des Tutorials, dein Kommentar ist richtig, ich lösche diese Zeile . Aber das löst das Problem nicht .... – Julo0sS
Abonnement nicht vom Konstruktor entfernen ... von "ngOnInit" entfernen – Rahul