Angular Material MdSidebar-如何从打字稿中调用.toggle()

尼古拉斯·弗登(Nicholas Foden)

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

AppComponent.html:1错误TypeError:AppComponent.webpackJsonp.176.AppComponent.toggleNav中的this.sidenav.toggle不是函数(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();
    }
}

app.component.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,它会抛出:

错误TypeError:this.sideNav.toggle不是AppComponent.webpackJsonp.176.AppComponent.toggleNav的函数(http://127.0.0.1:4200/main.bundle.js:171:22

周杰伦

AppComponent上的模板ref #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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何为 Angular Material Slide Toggle 设置默认值?

如何更改Angular Material的mat-slide-toggle属性?

Angular2-如何从打字稿中获取变量到我的HTML dom

ngIf可能会从打字稿中引用Angular组件时,该如何引用呢?

如何在Angular 6中将值从打字稿代码传递到CSS

如何从打字稿或angular4+中的当前日期计算旧日期

将初始值设置为HTML中Angular Material中的窗体的Toggle

如何使用Angular Material <mat-button-toggle>绑定到模型?

Angular 10-对Angular Material使用bootstrap-toggle样式

如何更改Material-UI的Toggle的颜色

Angular2 Material2-如何在表单重建中禁用/启用mat-datepicker-toggle

Angular 2 Material 2 Slide Toggle似乎无法在Alpha 7中工作错误:找不到名称“ HammerInput”

从打字稿(Angular/React)中的数据填充列中的索引值

Angular如何显示从打字稿到HTML的字符串

从打字稿和Angular中的Observable <string>获取字符串值

是否可以从打字稿Angular 5中设置<mat-hint>文本颜色

如何在Material-ui @ next中导入Toggle

从打字稿更改Angular Map svg标记的颜色

mat-slide-toggle Angular Material以编程方式切换

Angular Material 2 mat-slide-toggle 改变拇指滑动距离

如何从常规打字稿类(非组件)中调用angular 2+服务

如何覆盖 Angular Material Purple?

如何从Material Angular应用样式

如何重叠 Angular Material 项目

如何验证Angular Material Datepicker?

Angular Material 中的标签按钮

Material UI 中对齐的打字稿错误

如何在打字稿中对数组内的总价格求和并在angular 7中调用它

如何使用Angular Material 2创建自己的Material模块?