React AJAX在'/'上加载简单响应需要几分钟,但在'/ home'上需要几毫秒

时间:2016-04-17 07:50:10

标签: javascript jquery ajax reactjs jwt

我的反应代码获取帐户信息(电子邮件,姓名,图片等..)当我加载/登录时,加载并带回json响应需要几分钟,但在/ home时需要几毫秒。 / home扩展的组件:

constructor() {
        super();

        this.state = this._getAccountState();
    }

    _getAccountState() {
        return {
            account: AccountStore.account
        };
    }

    componentDidMount() {
        if(!this.state.account) {
            AccountService.getAccountInfo();
        }

        this.changeListener = this._onChange.bind(this);

        AuthStore.addChangeListener(this.changeListener);
        AccountStore.addChangeListener(this.changeListener);
    }

    _onChange() {
        this.setState(this._getAccountState());
    }

    componentWillUnmount() {
        AuthStore.removeChangeListener(this.changeListener);
        AccountStore.removeChangeListener(this.changeListener);
    }

用户登录时触发的服务,或者用户通过身份验证并加载应用程序的行为:

    getAccountInfo() {
    request({
        url: ACCOUNT_INFO_URL,
        method: 'get',
        crossOrigin: true,
        headers: {
            'Authorization': 'Bearer ' + AuthStore.jwt
        }
    })
        .then(function(response) {
            AccountActions.getAccountInfo(response);
        });
    }

触发应用调度程序的帐户操作:

getAccountInfo: (account) => {
    AppDispatcher.dispatch({
        actionType: GET_ACCOUNT_INFO,
        account: account
    })
}

AccountStore:

class AccountStore extends BaseStore {
constructor() {
    super();

    this.subscribe(() => this._registerToActions.bind(this));

    this._account = null;
}

_registerToActions(action) {
    switch(action.actionType) {
        case GET_ACCOUNT_INFO:
            this._account = action.account;

            this.emitChange();

            break;

        case LOGOUT_ACCOUNT:
            this._account = null;

            this.emitChange();

            break;

        default:
            break;
    }
}

get account() {
    return this._account;
}
}  

我不知道为什么它在索引页面中点击的第二个功能。

0 个答案:

没有答案