Utilizing a BehaviorSubject in my service for mat-sidenav toggle has proven to be effective.
toggle-service.ts
export class ToggleSidenavService {
public sideNavToggleSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
public toggle() {
return this.sideNavToggleSubject.next(null);
}
}
Component.html
@ViewChild('sidenav', {static: true}) public sidenav: MatSidenav;
ngOnInit() {
this.sidenavService.sideNavToggleSubject.subscribe(() => {
this.sidenav.toggle();
});
}
Although the Sidenav toggle functionality is working as intended, it consistently opens when the page first loads. How can I address this issue?