Angular Material UI MatSidenav 错误:无法设置未定义的 属性 'fixedTopGap'
Angular Material UI Error in MatSidenav : Cannot set property 'fixedTopGap' of undefined
我尝试使用 Angular 11.But 创建一个 angular material 工具栏和 sidenav,当我尝试设置 sidenav 的 属性 时,如下所示,它给出了控制台出现以下错误,导致菜单列表项中的第一个 (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>
好的,我在 angular 11 中找到了解决方案,如下所示更改视图子项的 sidenav 声明后
@ViewChild('sidenav',{static:true}) sidenav: MatSidenav;
如果您找到更好的方法,请告诉我:)
我尝试使用 Angular 11.But 创建一个 angular material 工具栏和 sidenav,当我尝试设置 sidenav 的 属性 时,如下所示,它给出了控制台出现以下错误,导致菜单列表项中的第一个 (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>
好的,我在 angular 11 中找到了解决方案,如下所示更改视图子项的 sidenav 声明后
@ViewChild('sidenav',{static:true}) sidenav: MatSidenav;
如果您找到更好的方法,请告诉我:)