Search code examples
htmlangularangular-materialangular11

Angular Material UI Error in MatSidenav : Cannot set property 'fixedTopGap' of undefined


I was trying to create an angular material toolbar and sidenav using Angular 11.But when i try to set the property of sidenav as shown below, its giving the following error in. console, and it is causing the first of the menu list item(mat-nav-list) not loading in the initial page load.

ERROR TypeError: Cannot set property 'fixedTopGap' of undefined
at AppComponent.ngOnInit (app.component.ts:19)
at callHook (core.js:2535)
at callHooks (core.js:2506)
at executeInitAndCheckHooks (core.js:2457)
at refreshView (core.js:9433)
at renderComponentOrTemplate (core.js:9532)
at tickRootContext (core.js:10758)
at detectChangesInRootView (core.js:10783)
at RootViewRef.detectChanges (core.js:22751)
at ApplicationRef.tick (core.js:29491)

My app.component.ts is given below

import { Component,ViewChild, HostListener, OnInit } from '@angular/core';
import { MatSidenav } from '@angular/material/sidenav';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'ui-material';
  opened = true;
  @ViewChild('sidenav') sidenav: MatSidenav;

  ngOnInit() {
    console.log(window.innerWidth)
    if (window.innerWidth < 768) {
      this.sidenav.fixedTopGap = 55;
      this.opened = false;
    } else {
      this.sidenav.fixedTopGap = 55;
      this.opened = true;
    }
  }

@HostListener('window:resize', ['$event'])
onResize(event:any) {
  if (event.target.innerWidth < 768) {
    this.sidenav.fixedTopGap = 55;
    this.opened = false;
  } else {
    this.sidenav.fixedTopGap = 55
    this.opened = true;
  }
}

isBiggerScreen() {
  const width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  if (width < 768) {
    return true;
  } else {
    return false;
  }
}

app.component.html

<mat-toolbar color="primary" class="header">
  <div>Organization Services</div>
  <span class="nav-tool-items">
    <mat-icon (click)="sidenav.toggle()" class="hamburger">menu</mat-icon>
  </span>
</mat-toolbar>

<mat-sidenav-container autosize>
  <mat-sidenav #sidenav [mode]="isBiggerScreen() ? 'over' : 'side'" [(opened)]="opened" [fixedInViewport]="true"
    [fixedTopGap]>
    <mat-nav-list>
      <a mat-list-item routerLinkActive="active" routerLink="/search">
        <mat-icon>search</mat-icon> Search
      </a>
      <a mat-list-item routerLinkActive="active" routerLink="/unit">
        <mat-icon>add_business</mat-icon>Unit
      </a>
      <a mat-list-item routerLinkActive="active" routerLink="/add">
        <mat-icon>person</mat-icon> Add
      </a>
    </mat-nav-list>
  </mat-sidenav>
  <mat-sidenav-content>
    <router-outlet></router-outlet>
  </mat-sidenav-content>
</mat-sidenav-container>

Solution

  • Ok i found the solution, in angular 11 after changing the sidenav declaration of view child as below

      @ViewChild('sidenav',{static:true}) sidenav: MatSidenav;
    

    Let me know if you find any better approach :)