mat-sidenav在页面加载时打开。我怎么关闭它?

时间:2018-06-02 09:39:54

标签: angular angular-material2 mat-sidenav

页面加载时

mat-sidenav已打开。我该如何关闭?

这是我的HTML:

<mat-sidenav
    #drawer
    class="sidenav" position="end"
    fixedInViewport="true"
    [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
    [mode]="(isHandset$ | async) ? 'over' : 'side'"
    [opened]="!(isHandset$ | async)">
    <mat-toolbar color="warn">Menu</mat-toolbar>
    <mat-nav-list>
      <a mat-list-item [routerLink] = "['/brands']"> Brands </a>
      <a mat-list-item [routerLink] = "['/variants']"> Variants </a>
    </mat-nav-list>
</mat-sidenav>

另外,当我点击页面中的任何位置时,我希望它接近(打开时)。目前只有在我点击菜单按钮时它才会关闭。

2 个答案:

答案 0 :(得分:0)

试试以下,

<mat-sidenav-container class="sidenav-container">
  <mat-sidenav
    #drawer
    class="sidenav"
    fixedInViewport="true"
    [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
    [mode]="(isHandset$ | async) ? 'over' : 'side'"
    [opened]="!(isHandset$ | async)">
    <mat-toolbar color="warn">Menu</mat-toolbar>
    <mat-nav-list>
      <a mat-list-item [routerLink] = "['/brands']"> Brands </a>
      <a mat-list-item [routerLink] = "['/variants']"> Variants </a>
    </mat-nav-list>
  </mat-sidenav>
  <mat-sidenav-content>
    <mat-toolbar color="primary">
      <button
        type="button"
        aria-label="Toggle sidenav"
        mat-icon-button
        (click)="drawer.toggle()"
        *ngIf="isHandset$ | async">
        <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
      </button>
      <span>Application Title</span>
    </mat-toolbar>
  </mat-sidenav-content>
</mat-sidenav-container>

答案 1 :(得分:0)

您可以尝试它对我的身体很好

<mat-nav-list>
  <a mat-list-item [routerLink] = "['/brands']" (click)="drawer.close()"> Brands </a>
  <a mat-list-item [routerLink] = "['/variants']" (click)="drawer.close()"> Variants </a>
</mat-nav-list>