我不知道为什么会收到未定义的错误。这是我收到的错误消息:“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 定义为空对象,它将起作用。
关于javascript - 未定义错误 Angular 2 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881764/