If you are working in Angular 9+ Remember add param of static: true
in @ViewChild
like:
@ViewChild('rightSidenav', {static: true}) sidenav: MatSidenav;
You can see my code working on Angular 9 here:
https://stackblitz.com/edit/angular-kwfinn-matsidenav-angular9