javascript 按钮为自推式(JS)

tsm1rwdh  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(138)

注销按钮类,我正在创建一个示例来连接它与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);
0lvr5msh

0lvr5msh1#

问题出在最后一行newLogoutButton.action = ApiConnector.logout(logoutSuccess);
将其更改为newLogoutButton.action = () => ApiConnector.logout(logoutSuccess);

csbfibhn

csbfibhn2#

最后一行必须是函数,现在只需立即调用.logout()

newLogoutButton.action = () => ApiConnector.logout(logoutSuccess);

相关问题