Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2:如何防止两种方法重复_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 角度2:如何防止两种方法重复

Javascript 角度2:如何防止两种方法重复,javascript,angular,typescript,Javascript,Angular,Typescript,如何只实现一次我的if/else语句,这样我就不会在标题组件中重复这段代码,如下所示: export class HeaderMainComponent { logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts @ViewChild('navTrigger') navTrigger: ElementRef; isMenuShown: false; constructor(private layoutServ

如何只实现一次我的
if/else
语句,这样我就不会在标题组件中重复这段代码,如下所示:

export class HeaderMainComponent {
logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts

@ViewChild('navTrigger') navTrigger: ElementRef;

isMenuShown: false;

constructor(private layoutService: LayoutService, private renderer: Renderer) { }

menuToggle(event: any) {
    if (this.navTrigger.nativeElement.classList.contains('opened')) {
        this.navTrigger.nativeElement.classList.remove('opened');
    } else {
        this.navTrigger.nativeElement.classList.add('opened');
    }
}

onMenuSelect(event: any) {
    this.isMenuShown = false;

    if (this.navTrigger.nativeElement.classList.contains('opened')) {
        this.navTrigger.nativeElement.classList.remove('opened');
    } else {
        this.navTrigger.nativeElement.classList.add('opened');
    }
}

}

你不就这么做吗

export class HeaderMainComponent {
logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts

@ViewChild('navTrigger') navTrigger: ElementRef;

isMenuShown: false;

constructor(private layoutService: LayoutService, private renderer: Renderer) { }

menuToggle(event: any) {
    if (this.navTrigger.nativeElement.classList.contains('opened')) {
        this.navTrigger.nativeElement.classList.remove('opened');
    } else {
        this.navTrigger.nativeElement.classList.add('opened');
    }
}

onMenuSelect(event: any) {
    this.isMenuShown = false;

    this.menuToggle(event); // Must apply the event binding as well
}

}

你不就这么做吗

export class HeaderMainComponent {
logoAlt = 'We Craft beautiful websites'; // Logo alt and title texts

@ViewChild('navTrigger') navTrigger: ElementRef;

isMenuShown: false;

constructor(private layoutService: LayoutService, private renderer: Renderer) { }

menuToggle(event: any) {
    if (this.navTrigger.nativeElement.classList.contains('opened')) {
        this.navTrigger.nativeElement.classList.remove('opened');
    } else {
        this.navTrigger.nativeElement.classList.add('opened');
    }
}

onMenuSelect(event: any) {
    this.isMenuShown = false;

    this.menuToggle(event); // Must apply the event binding as well
}

}

在这之后,您不能从onMenuSelect调用menutogle.ismenushighed=false;声明?为什么不使用模板绑定(
ngClass
)来代替o_o?在此之后,您不能从onMenuSelect调用menutogle。ismenushowed=false;Station?为什么不使用模板绑定(
ngClass
)而不是o_o?Typescript发出错误
提供的参数与调用目标的任何签名都不匹配。
问题已修复,我们也需要绑定事件-
此.menutogle(事件)。请有人告诉我这是否是正确的说法。Typescript发出错误
提供的参数与调用目标的任何签名都不匹配。
问题已修复,我们也需要绑定事件-
此.menutogle(事件)。请有人告诉我这样说是否正确。