注销按钮类,我正在创建一个示例来连接它与API。示例创建后,它立即被激活,就像按钮被按下,你能帮助了解什么是错误的吗?我只是初学者
class LogoutButton {
constructor() {
[this.logoutBtn] = document.getElementsByClassName('logout');
this.action = (f) => f;
this.logoutBtn.addEventListener('click', this.logoutClick.bind(this));
}
logoutClick(event) {
event.preventDefault();
this.action();
}
}
示例
"use strict"
let newLogoutButton = new LogoutButton();
const logoutSuccess = (data) => {
if (data.success === true) {
location.reload();
} else {
alert("");
}
};
newLogoutButton.action = ApiConnector.logout(logoutSuccess);
2条答案
按热度按时间0lvr5msh1#
问题出在最后一行
newLogoutButton.action = ApiConnector.logout(logoutSuccess);
将其更改为
newLogoutButton.action = () => ApiConnector.logout(logoutSuccess);
csbfibhn2#
最后一行必须是函数,现在只需立即调用
.logout()