如何从另一个组件切换 sidenav?
当按钮/触发器与sidenav在同一个.html文件中时,它工作得很好,但如果我生成一个新组件,在我的例子中,我将它们命名为“sidebar-left”和“topbar”,它不起作用.
我所做的是生成组件来分离它们。我将sidenav放在“sidebar-left”组件中,将按钮/触发器放在“topbar”组件中。
侧边栏-left.component.html
<mat-sidenav-container>
<mat-sidenav #sidenav mode="side" [(opened)]="opened" opened>
<h3>Sidenav</h3>
</mat-sidenav>
<mat-sidenav-content>
<app-dashboard></app-dashboard>
</mat-sidenav-content>
</mat-sidenav-container>
topbar.component.html
<mat-toolbar>
<button (click)="sidenav.toggle()" mat-raised-button>Toggle Sidenav</button>
</mat-toolbar>
无论我在应用程序中的哪个位置触发它,我都希望能够触发 sidenav。
当我按下切换按钮时,我遇到了这个错误:
TopbarComponent.html:1 ERROR TypeError: Cannot read property 'toggle' of undefined
at Object.eval [as handleEvent] (TopbarComponent.html:2)
at handleEvent (core.js:43993)
at callWithDebugContext (core.js:45632)
at Object.debugHandleEvent [as handleEvent] (core.js:45247)
at dispatchEvent (core.js:29804)
at core.js:42925
at HTMLButtonElement.<anonymous> (platform-browser.js:2668)
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)