有没有办法检查sidenav
元素是否打开?根据API文档,有一个isOpen?
参数,但就我而言,它会引发异常:EXCEPTION: Error in ./AppComponent class AppComponent - inline template:2:2 caused by: this.sidenav.isOpen is not a function
import { Component, OnInit, ElementRef } from '@angular/core'; @Component({ selector: 'app-sidebar', template: `Sidenav `, styleUrls: ['./sidebar.component.css'], host: { '(document:click)': 'onClick($event)', } }) export class SidebarComponent implements OnInit { constructor(private sidenav: ElementRef) { } ngOnInit() { } onClick() { if (this.sidenav.isOpen()) { this.sidenav.close(); } } }
Pankaj Parka.. 8
您必须用来ViewChild
控制sidenav
@ViewChild('sidenav') sidenav: any; opened: any = false; onClick() { console.log(this.sidenav.opened); }
普伦克
您必须用来ViewChild
控制sidenav
@ViewChild('sidenav') sidenav: any; opened: any = false; onClick() { console.log(this.sidenav.opened); }
普伦克