3

我试图让 Side Material Sidenav 在我打开栏时推送内容,显然在它关闭时;被推回原位的元素。目前它所做的是对内容开放

看图片

这是我的 app-header.component.html

<!--- Toolbar starts here-->
<div class="container-toolbar">
    <mat-toolbar color="primary" class="fixed-header">
        <img src="" />
        <button type="button" aria-label="Toggle sidenav" mat-icon-button (click)="drawer.toggle()">
            <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
        </button>
        <span class="spacer"></span>
        <button type="button" mat-icon-button href="">
            <mat-icon matTooltip="Salir">exit_to_app</mat-icon>
        </button>
    </mat-toolbar>
</div>

<!--- Sidenav starts here -->
<mat-sidenav-container style="height:100%;width:100%;">
    <mat-sidenav #drawer fixedInViewport="true" [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'" [mode]="(isHandset$ | async) ? 'side' : 'push'"
        [opened]="(isHandset$ | async)" style="box-shadow: 0 5px 5px #999;z-index: 1;">
        <mat-nav-list>
            <mat-list-item>
                <a routerLink="/dashboard">Home</a>
                <mat-icon mat-list-icon>home</mat-icon>
            </mat-list-item>
            <mat-list-item>
                <a routerLink="/parametros">Section 2</a>
                <mat-icon mat-list-icon>tune</mat-icon>
            </mat-list-item>
            <mat-list-item>
                <a routerLink="/#">Section 3</a>
                <mat-icon mat-list-icon>settings</mat-icon>
            </mat-list-item>
            <mat-list-item>
                <a routerLink="/#">Section 4</a>
                <mat-icon mat-list-icon>layers</mat-icon>
            </mat-list-item>
            <mat-list-item (click)="showSubmenu = !showSubmenu" class="parent">
                <span class="full-width" *ngIf="isExpanded || isShowing">Dropdown</span>
                <mat-icon mat-list-icon>flash_on</mat-icon>
                <mat-icon class="menu-button" [ngClass]="{'rotated' : showSubmenu}" *ngIf="isExpanded || isShowing">expand_more</mat-icon>
            </mat-list-item>
            <div class="submenu" [ngClass]="{'expanded' : showSubmenu}" *ngIf="isShowing || isExpanded">
                <mat-list-item>
                    <a routerLink="/#">Section Dropdown 1</a>
                </mat-list-item>
                <mat-list-item>
                    <a routerLink="/#">Section Dropdown 2</a>
                </mat-list-item>
                <h2 matSubheader class="mat-submenu-title">
                    <mat-icon>account_balance</mat-icon>Title Dropdown
                </h2>
                <mat-list-item (click)="showSubSubMenu = !showSubSubMenu" class="parent">
                    <span class="full-width" *ngIf="isExpanded || isShowing">Dropdown 2 (inside)</span>
                    <mat-icon class="menu-button" [ngClass]="{'rotated' : showSubSubMenu}" *ngIf="isExpanded || isShowing">expand_more</mat-icon>
                </mat-list-item>
                <div class="submenu" [ngClass]="{'expanded' : showSubSubMenu}" *ngIf="isShowing || isExpanded">
                    <mat-list-item>
                        <a routerLink="/topupcard">Dropdown section</a>
                    </mat-list-item>
                    <mat-list-item>
                        <a routerLink="/#">Dropdown section</a>
                    </mat-list-item>
                    <h2 matSubheader class="mat-submenu-title">
                        <mat-icon>card_travel</mat-icon> Title
                    </h2>
                    <mat-list-item>
                        <a routerLink="/#">Dropdown 2</a>
                    </mat-list-item>
                    <mat-list-item>
                        <a routerLink="/#">Dropdown 2</a>
                    </mat-list-item>
                </div>
            </div>
        </mat-nav-list>
    </mat-sidenav>
    <mat-sidenav-content></mat-sidenav-content>
</mat-sidenav-container>

我的 app-header.component.ts

import { Component, ViewChild } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { map } from 'rxjs/operators';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import { User } from '../../resources/user.resource';
import { UserService } from '../../services/user.service';
import { MatSidenav } from '@angular/material/sidenav';

@Component({
    selector: 'app-header',
    templateUrl: './app-header.component.html',
    styleUrls: ['./app-header.component.css']
})
export class AppHeader {
    isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
        .pipe(map(result => result.matches));

    constructor(private breakpointObserver: BreakpointObserver) { }

    @ViewChild('sidenav') sidenav: MatSidenav;
    isExpanded = true;
    showSubmenu = false;
    isShowing = false;
    showSubSubMenu = false;

    onmouseenter() {
        if (!this.isExpanded) {
            this.isShowing = true;
        }
    }

    onmouseleave() {
        if (!this.isExpanded) {
            this.isShowing = false;
        }
    }
}

我的 app.component.html

<app-header></app-header>
<div class="app-body">
    <main class="main">
        <div class="container-fluid">
            <router-outlet></router-outlet>
            <ngx-loading [show]="loading"></ngx-loading>
        </div>
    </main>
</div>
4

3 回答 3

4

你可以autosize使用mat-sidenav-container

模板

<mat-sidenav-container autosize>
  <mat-sidenav #sidenav class="example-sidenav" mode="side" opened="true">

    <mat-nav-list>
      <mat-list-item>
        <button mat-icon-button (click)="isExpanded = !isExpanded">
          <mat-icon *ngIf="!isExpanded">chevron_right</mat-icon>
          <mat-icon *ngIf="isExpanded">chevron_left</mat-icon>
        </button>
      </mat-list-item>
      <mat-list-item>
       <mat-icon mat-list-icon>home</mat-icon>
         <p matLine *ngIf="isExpanded">Home</p>
      </mat-list-item>
    </mat-nav-list>
  </mat-sidenav>

  <div class="example-sidenav-content">
    Main content that resizes properly
  </div>

</mat-sidenav-container>

零件

@Component({
  selector: 'sidenav-autosize-example',
  templateUrl: 'sidenav-autosize-example.html',
  styleUrls: ['sidenav-autosize-example.css'],
})
export class SidenavAutosizeExample {
  isExpanded = false;
}

在 stackblitz 上查看

于 2018-12-13T10:25:51.730 回答
0

你不需要[]参数。代替:

[mode]="(isHandset$ | async) ? 'side' : 'push'" 

经过:

mode="side"

您可能必须在 上添加autosizemat-sidenav-container,如下所示:

<mat-sidenav-container autosize>
于 2018-12-13T10:26:07.590 回答
0

固定:只需添加

<router-outlet></router-outlet>

<mat-sidenav-content></mat-sidenav-content>标签内部,在 app-header.component.html 文件中。谢谢大家的回答!

于 2018-12-13T12:50:53.107 回答