Reputation: 2496
I have an app with angular, it display the menu after successfully logged in work great.
So i have GlobalEventsManager
@Injectable()
export class GlobalEventsManager {
private _showNavBar: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
public showNavBarEmitter: Observable<boolean> = this._showNavBar.asObservable();
constructor() {}
showNavBar(ifShow: boolean) {
this._showNavBar.next(ifShow);
}
}
And in menu component
this.globalEventsManager.showNavBarEmitter.subscribe((mode)=>{
if (mode !== null) {
this.showNavBar = mode;
}
});
And In login
this.globalEventsManager.showNavBar(true);
this.router_.navigate(['welcome']);
I put menu inside app.component
<app-menu></app-menu>
<router-outlet></router-outlet>
So menu work great, but after refresh page menu disappear, in console log i get console.log(mode) null.
So how can i resolve this issue and thanks
Upvotes: 0
Views: 2577
Reputation: 5435
That's normal that after page refresh _showNavBar
value is being set to null
again. That's because the whole GlobalEventsManager
has run again. To resolve that, you need to check whether the user is logged in or not (for example inside constructor in GlobalEventsManager
and then set _showNavBar
value once more.
Best place for checking whether user is logged in or not would be some global component.
Upvotes: 1