gpt4 book ai didi

angular - 使用 ViewChild 访问 MatDrawer 在 Angular 8 中抛出错误

转载 作者:行者123 更新时间:2023-12-05 03:00:58 25 4
gpt4 key购买 nike

我正在尝试使用 Angular 版本 8 中 MatDrawer 类型的属性通过组件文件设置 MatSideNav 的行为

TypeScript 代码:

// Declaration
@ViewChild('drawer', { static: false }) public drawer: MatDrawer;

// Toggle the Side Nav bar
showSideNav(): void {
this.drawer.toggle();
}

HTML 代码:

<mat-drawer-container>
<mat-drawer #drawer>
<div>Side nav bar content</div>
</mat-drawer>
<div>Main content</div>
</mat-drawer-container>

控制台错误:

ERROR TypeError: "this.drawer is undefined"

请帮助我如何使用 @ViewChild 访问 MatDrawer 元素

最佳答案

更改声明自

@ViewChild('drawer', { static: false }) public drawer: MatDrawer;

@ViewChild('drawer', { static: true }) public drawer!: MatDrawer;

关于angular - 使用 ViewChild 访问 MatDrawer 在 Angular 8 中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com