繁体   English   中英

未定义错误Angular 2类

[英]Undefined error Angular 2 class

我不知道为什么会收到未定义的错误。 这是我收到的错误消息:“ TypeError:无法设置未定义的属性'状态'

这是我的代码:

export class MobileMenuComponent implements OnInit {

menu: any;

constructor() { 


}

ngOnInit() { 

    this.menu.state = 'inactive';

    this.menu.togglemenu = function() {

        if (this.menu.state === 'inactive'){

            this.menu.state = 'active';

        }

        else {

            this.menu.state = 'inactive';

        }

    }

}


}

在构造函数中将此this.menu定义为空对象,它将起作用。

this内部的togglemenu函数-用于函数本身,它不包含菜单成员。 您应该在this内部函数上方使用。 像这样:

var that = this;
this.menu.togglemenu = function() {
   if (that.menu.state === 'inactive'){
            that.menu.state = 'active';
       }
        else {
            that.menu.state = 'inactive';
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM