3

我正在尝试使用 Angular 8 中 MatDrawer 类型的属性通过组件文件设置 MatSideNav 的行为

打字稿代码:

// Declaration
@ViewChild('drawer', { static: false }) public drawer: MatDrawer;

// Toggle the Side Nav bar
showSideNav(): void {
    this.drawer.toggle();
}

HTML 代码:

<mat-drawer-container>
      <mat-drawer #drawer>
        <div>Side nav bar content</div>
      </mat-drawer>
      <div>Main content</div>
</mat-drawer-container>

控制台错误:

ERROR TypeError: "this.drawer is undefined"

请帮助我如何MatDrawer使用@ViewChild

4

1 回答 1

4

更改声明从

@ViewChild('drawer', { static: false }) public drawer: MatDrawer;

@ViewChild('drawer', { static: true }) public drawer!: MatDrawer;
于 2021-01-09T12:40:58.440 回答