-1

I want my material menu to open automatically when my page is opened, but because I am not click, "trigger" is undefined

TypeError: Cannot read properties of undefined (reading 'openMenu')

 @ViewChild('menuTrigger') trigger;
    ngOnInit() {  
    if (this.headerService.isCookieDown = "0") {
          this.trigger.openMenu()
        } 
    }



 <button mat-button #menuTrigger="matMenuTrigger" [matMenuTriggerFor]="infomenu"><img bell.svg"></button>



 <mat-menu class="info-menu" style="max-width: 400px !important;" [hasBackdrop]="false" #infomenu="matMenu"> 
html codes..
 </mat-menu>
  • Consider moving the open menu call to the [`ngAfterViewInit`](https://angular.io/guide/lifecycle-hooks#responding-to-view-changes) lifecycle hook, rather than `ngOnInit`. – R. Richards Sep 10 '22 at 22:07
  • thank you sir it worked if you reply as a post I can mark it as correct – noNeedTagess Sep 10 '22 at 22:10

1 Answers1

-1

Implement the ngAfterViewInit lifecycle hook and make the openMenu call in ngAfterViewInit, like below.

ngAfterViewInit() {  
    if (this.headerService.isCookieDown = "0") {
      this.trigger.openMenu()
    } 
}
R. Richards
  • 24,603
  • 10
  • 64
  • 64