通过mat-sidenav-container外部的按钮打开mat-sidenav

时间:2019-02-27 10:38:14

标签: angular material sidenav

我尝试为Angular Material团队在this example上进行的移动用途做相同的事情。

所以我创建了我的组件,如下所示:

<app-component-0></app-component-0>
<div class="d-lg-none">
    <button mat-icon-button (click)="snav.toggle()">
         <mat-icon>menu</mat-icon>
    </button>
    <mat-sidenav-container class="sidenav-container">
        <mat-sidenav #snav mode="over">
           <app-menu-tree>
           </app-menu-tree>
        </mat-sidenav>
        <mat-sidenav-content>
           <router-outlet></router-outlet>
        </mat-sidenav-content>
   </mat-sidenav-container>
</div>
<app-component-1-desktop class="d-none d-lg-flex flex-column"></app-component-1-desktop>

因此,调用sidenav的切换动作的按钮在mat-sidenav-container的外部,就像在stackblitz的示例中一样... 但是,当我单击按钮时,出现以下错误:

  

无法读取未定义的属性“ toggle”

因此该按钮无法打开边导航...

有人有解决此问题的想法吗?

(抱歉我的英语,这不是我的母语)

1 个答案:

答案 0 :(得分:1)

使用opened输入。 API:https://material.angular.io/components/sidenav/api

模板:

<button mat-icon-button (click)="opened = !opened">
  <mat-icon>menu</mat-icon>
</button>
<mat-sidenav-container class="example-container">
  <mat-sidenav #sidenav mode="over" [(opened)]="opened">
    <app-menu-tree></app-menu-tree>
  </mat-sidenav>
  <mat-sidenav-content>
    <router-outlet></router-outlet>
  </mat-sidenav-content>
</mat-sidenav-container>

记住要在组件中定义opened属性。