【问题标题】:Angular Material UI Error in MatSidenav : Cannot set property 'fixedTopGap' of undefinedMatSidenav 中的 Angular Material UI 错误:无法设置未定义的属性“fixedTopGap”
【发布时间】:2021-04-04 03:37:10
【问题描述】:

我试图使用 Angular 11 创建一个有角度的材质工具栏和 sidenav。但是当我尝试如下所示设置 sidenav 的属性时,它在.console 中给出了以下错误,它导致了菜单的第一个列表项(mat-nav-list)未在初始页面加载中加载。

ERROR TypeError: Cannot set property 'fixedTopGap' of undefined
at AppComponent.ngOnInit (app.component.ts:19)
at callHook (core.js:2535)
at callHooks (core.js:2506)
at executeInitAndCheckHooks (core.js:2457)
at refreshView (core.js:9433)
at renderComponentOrTemplate (core.js:9532)
at tickRootContext (core.js:10758)
at detectChangesInRootView (core.js:10783)
at RootViewRef.detectChanges (core.js:22751)
at ApplicationRef.tick (core.js:29491)

我的 app.component.ts 如下所示

import { Component,ViewChild, HostListener, OnInit } from '@angular/core';
import { MatSidenav } from '@angular/material/sidenav';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'ui-material';
  opened = true;
  @ViewChild('sidenav') sidenav: MatSidenav;

  ngOnInit() {
    console.log(window.innerWidth)
    if (window.innerWidth < 768) {
      this.sidenav.fixedTopGap = 55;
      this.opened = false;
    } else {
      this.sidenav.fixedTopGap = 55;
      this.opened = true;
    }
  }

@HostListener('window:resize', ['$event'])
onResize(event:any) {
  if (event.target.innerWidth < 768) {
    this.sidenav.fixedTopGap = 55;
    this.opened = false;
  } else {
    this.sidenav.fixedTopGap = 55
    this.opened = true;
  }
}

isBiggerScreen() {
  const width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  if (width < 768) {
    return true;
  } else {
    return false;
  }
}

app.component.html

<mat-toolbar color="primary" class="header">
  <div>Organization Services</div>
  <span class="nav-tool-items">
    <mat-icon (click)="sidenav.toggle()" class="hamburger">menu</mat-icon>
  </span>
</mat-toolbar>

<mat-sidenav-container autosize>
  <mat-sidenav #sidenav [mode]="isBiggerScreen() ? 'over' : 'side'" [(opened)]="opened" [fixedInViewport]="true"
    [fixedTopGap]>
    <mat-nav-list>
      <a mat-list-item routerLinkActive="active" routerLink="/search">
        <mat-icon>search</mat-icon> Search
      </a>
      <a mat-list-item routerLinkActive="active" routerLink="/unit">
        <mat-icon>add_business</mat-icon>Unit
      </a>
      <a mat-list-item routerLinkActive="active" routerLink="/add">
        <mat-icon>person</mat-icon> Add
      </a>
    </mat-nav-list>
  </mat-sidenav>
  <mat-sidenav-content>
    <router-outlet></router-outlet>
  </mat-sidenav-content>
</mat-sidenav-container>

【问题讨论】:

    标签: html angular angular-material angular11


    【解决方案1】:

    好的,我找到了解决方案,在更改 view child 的 sidenav 声明后,角度 11 如下

      @ViewChild('sidenav',{static:true}) sidenav: MatSidenav;
    

    如果您找到更好的方法,请告诉我:)

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2017-04-26
      • 1970-01-01
      • 2018-06-18
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多