gpt4 book ai didi

javascript - Angular Material MdSidebar - 如何从 typescript 调用 .toggle()

转载 作者:太空狗 更新时间:2023-10-29 18:13:32 25 4
gpt4 key购买 nike

我正在尝试从另一个组件进行 sidenav 切换,但是当从我的父组件调用 .toggle() 函数时,它会抛出此错误:

AppComponent.html:1 ERROR TypeError: this.sidenav.toggle is not a function at AppComponent.webpackJsonp.176.AppComponent.toggleNav (app.component.ts:25)

这里是代码:app.component.ts

import { Component, ViewChild } from '@angular/core';
import { MaterialModule } from '@angular/material';
import { NavComponent } from './nav/nav.component';
import { SidebarComponent } from './sidebar/sidebar.component';
import { FooterComponent } from './footer/footer.component';
import { MdSidenav } from '@angular/material';
@Component({
moduleId: module.id,
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent{
title = 'CRS Management App';
@ViewChild('sidenav') sidenav:MdSidenav;
toggleNav(){
this.sidenav.toggle();
}
}

应用程序组件.html:

<app-nav #navbar (nav)="toggleNav()"></app-nav>
<app-sidebar #sidenav></app-sidebar>
<app-footer></app-footer>

(nav) 是从 app-nav 组件发出的。

sidebar.component.ts:

@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.scss']
})
export class SidebarComponent {
title = 'CRS';

constructor() { }

}

sidebar.component.html:

 <md-sidenav-container class="container">
<!--SIDEBAR -->
<md-sidenav #sidebar class="sidebar" mode="over" opened="true">

<!-- MENU https://material.angular.io/components/component/list -->
<div class="nav-links">
<md-nav-list> <!--TODO Links -->
<a md-list-item href="/one"> Option </a> <!--TODO Icon -->
<a md-list-item href="/two"> Option </a> <!--TODO Icon-->
<a md-list-item href="/three"> Option </a> <!--TODO Icon-->
<span class="flex"></span><!--TODO Divider-->
<a md-list-item href="/four"> _______ </a> <!--TODO Icon-->
<a md-list-item href="/five"> Option </a> <!--TODO Icon -->
<a md-list-item href="/six"> Option </a> <!--TODO Icon-->
<a md-list-item href="/seven"> Option </a> <!--TODO Icon-->
<a md-list-item href="/eight"> Option </a> <!--TODO Icon-->
</md-nav-list>
<!-- SETTINGS -->
<!--TODO link to settings -->
<!--TODO Convert to md-list-item -->
<button md-button class="md-icon-button" aria-label="Settings">
Settings<md-icon>settings</md-icon>
</button>
<!-- SETTINGS END -->
</div>

</md-sidenav>
<!-- SIDEBAR ENDS -->
<router-outlet></router-outlet>
</md-sidenav-container>

我试过使用 AfterViewInit,它抛出:

ERROR TypeError: this.sideNav.toggle is not a function at AppComponent.webpackJsonp.176.AppComponent.toggleNav (http://127.0.0.1:4200/main.bundle.js:171:22)

最佳答案

AppComponent 上的模板引用 #sidenav 指的是没有切换功能的 SidebarComponent。

您可以向其中添加一个,然后调用 sidenav.toggle(),然后您可以从 AppComponent 中调用它。

sidebar.component.ts

  import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSidenav} from '@angular/material';

@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {
title = 'CRS';
@ViewChild('sidenav') sidenav: MatSidenav;
constructor() { }

ngOnInit() {
}

toggle() {
this.sidenav.toggle();
}
}

sidebar.component.html

  <md-sidenav-container class="container">
<md-sidenav #sidenav class="sidebar" mode="over" opened="true">
...

</md-sidenav>
</md-sidenav-container>

关于javascript - Angular Material MdSidebar - 如何从 typescript 调用 .toggle(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598030/

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