为什么我的其他类函数和变量在jQuery click事件触发我的es6类函数之一后变得未定义

我正在运行es6类函数来重新设计我的身份验证页面。当用户单击“立即注册”链接时,应该运行showRegisterSection函数,以便它从auth页面的登录部分切换到注册部分。但是,问题在于,当我尝试访问在构造函数和该类的其他函数中初始化的局部变量以执行一些重新设计任务时,浏览器控制台将返回:

“ functionName不是函数”

有关功能和

“未定义”

变量的

值。

我尝试了不同的功能并在线搜索解决方案,但无济于事。此外,如果我将代码复制到函数内部,然后将其粘贴到事件处理程序showRegisterSection中,或在事件处理程序中重新分配变量,一切都会按预期进行。

请记住,设计登录部分的第一个函数称为: ShowLoginSection正常工作,不会从事件中调用。这也是设置调用showRegisterFunction的点击事件的功能。

下面提供了代码:

    removeElement,insert_custom_social_login_before_target_Element
 } from './util';

export default class GulaitAuth{
    //if the isLogin variable is true,then we set up the login part else,we set up the register part
    constructor(isLoginSection,loginForm){
        this._isLoginSection = isLoginSection;
        this._loginForm = loginForm;
        this.initLocalVars();
    }

    /**
     * This function initializes local variables to their supposed values
     * except those passed in on creation of class obj.
     * This is due to an unknown issue where variables simply get reset  to undefined
     */
    initLocalVars(){
        this._loginDiv = document.querySelector('.woocommerce #customer_login').firstElementChild;
        this._registerDiv = document.querySelector('.woocommerce #customer_login').lastElementChild;
        this._myaccHeader = document.querySelector( '.woocommerce-account #main #contents header h2' );
        this._socialLoginContainer = document.querySelector( '#customer_login .apsl-login-networks.theme-2.clearfix' );
        this._authPageLoginForm = document.querySelector( '.woocommerce-page #customer_login form.login' );
    }

    setupAuthPage(){
        //setup the authentication page
        //css styles for Auth page if the loginForm exists on the page
        if( this._loginForm ){
            //remove header "MY accOUNT" from DOM
            removeElement(this._myaccHeader);

            //center the auth div
            jQuery('body.page-id-29 .container .row.sidebar-row').css('text-align','center');

            //style the Auth div - container
            jQuery('.woocommerce-account #contents').css({
                'max-width' : '38.5em','display': 'inline-block','text-align': 'initial','padding': '1.5em','border-top' : '.15em solid #DF1F26e5'
            });

            if( this._isLoginSection ){
                this.showLoginSection();
            } else {
                this.showRegisterSection();
            }

        }
    }

    /**
     * Show Login or Register Section based on _isLoginSection variable
     */
    showLoginOrRegister(){
        if(this._isLoginSection){
            //edit the login div
            jQuery(this._loginDiv).css( {'min-width' : '100%','padding' : '0','display' : 'block'} );

            //hide the register div
            jQuery(this._registerDiv).css( 'display','none' );
        } else {
            //hide the login div
            jQuery(this._loginDiv).css( 'display','none' );

            //edit the register div
            jQuery(this._registerDiv).css( {'min-width' : '100%','display' : 'block'} );
        }

    }

    showLoginSection(){
        //show loginsection
        this.showLoginOrRegister();

        //remove full width on checkbox
        jQuery('.woocommerce #customer_login form.login input[type="checkbox"]').css('min-width','0 !important');

        //remove extra spacing to the right
        jQuery('.woocommerce-account #main #contents .type-page').css('padding','0');

        //row containing login and register forms is slightly pushed to the left with margin
        //this makes styling difficult as it has wierd positioning
        jQuery('div#customer_login.row').css('margin','0');

        //remove extra space after the 'lost password?' section
        jQuery('.entry .entry-content .entry-summary').css('margin-bottom','0');

        //remove login form margin
        jQuery('.woocommerce-page #customer_login form.login').css('margin','0');

        //edit the login text
        jQuery('.woocommerce #customer_login h2')
        .css( { 
            'border-bottom' : '0','margin-bottom' : '0','padding-bottom' : '0.2em'
        } )
        .text('')
        .append(
            '<span id="login-text">Login</span><span id="or-text"> Or </span><span id="register-text">Register</span>'
        );

        jQuery('#or-text,#register-text').css( {
            'opacity' : '.2','font-size' : '16px','word-spacing' : '.12em','font-weight' : '450'
        } );


        //Insert REGISTRATION Link after 'lost password' section
        jQuery("<p style='margin-top: 2em;'>Don't have an account? <strong id='register-link'><a href='#'>Register now</a></strong></p>")
        .insertAfter("#customer_login .login p.lost_password")
        .children("#register-link").click( this.showRegisterSection );

        //add an eventListener on the register link
        //jQuery('#register-link');

        //redesign the input form input fields
        jQuery('#customer_login .login .form-row.form-row-wide input').css( {
            'border' : '1px solid #ccc','background' : 'white'
        } );

        //fb color = #1c74bc

        //Delete original social login and replace with custom version in the 
        //prefered position
        this.relocateSocialLogin();
    }

    showRegisterSection(){
        console.log('Register Link clicked');

        //set isLogin to false in order to show content as designed for the register form
        this._isLoginSection = false;

        //This function initializes local variables to their supposed values except
        //those passed in on creation of class obj. 
        //This is due to an unknown issue where variables simply get reset to undefined
        this.initLocalVars();

        //show register section
        //hide the login div
        jQuery(this._loginDiv).css( 'display','none' );

        //edit the register div
        jQuery(this._registerDiv).css( {'min-width' : '100%','display' : 'block'} );
    }

    /**
     * Delete original social login and replace with custom version in the 
     * prefered position to fit design
     */
    relocateSocialLogin(){
        //Delete existing social login
        this._socialLoginContainer.parentNode.removeChild( this._socialLoginContainer );

        //Insert custom social login just before login form on auth page = my-account page
        insert_custom_social_login_before_target_Element( this._authPageLoginForm );

    }

}
phoneix_l 回答:为什么我的其他类函数和变量在jQuery click事件触发我的es6类函数之一后变得未定义

您必须将this引用保留在showLoginSection()内定义的点击处理程序中

按照书面规定:.children("#register-link").click( showRegisterSection );

使用箭头功能:.children("#register-link").click( e => this.showRegisterSection(e)); 然后,将保留this中任何this的{​​{1}}引用。 (我们的身份验证实例)

替代方法是showLoginSection()的变体 因此,.bind()内的.children("#register-link").click( this.showRegisterSection.bind(this));

或在showLoginSection()中的this.showRegisterSection = this.showRegisterSection.bind(this);

对于将用作事件处理程序的所有函数,或在其他情况下未将该函数作为constructor(isLoginSection,loginForm){类的方法调用的其他情况,必须执行此操作。

本文链接:https://www.f2er.com/2719233.html

大家都在问