【问题标题】:Action ReactJs using Alt使用 Alt 的动作 ReactJ
【发布时间】:2016-06-29 17:33:09
【问题描述】:

我正在使用 Reactjs 构建第一个应用程序,并且我还使用 Alt 库来实现 Flux 模型。 我有一个问题,我试图解决它,但它没有工作。 这是我的代码:

import alt from '../alt';
class AddUserActions {
constructor() {
 this.generateActions(
  'addUserSuccess',
  'addUserFail'
  );}
   addUser(payload)
    {    
      $.ajax({
      type:'POST',
      url:'/api/user',
      data:{username:payload.username,
        password:payload.password,
        fbId:payload.fbId         
      }
    })
   .done((data) => {   
     this.actions.addUserSuccess(data.message);
    })
   .fail((jqXhr) =>{
   this.actions.addUserFail(jqXhr.responseJSON.message);
    });
  }
 }
 export default alt.createActions(AddUserActions);

这里,带有 data.message 的本地服务器响应是“成功”,状态 200。问题发生在 this.actions.addUserSuccess(data.message);。 错误:Uncaught TypeError: Cannot read property 'addUserSuccess' of undefined 我不知道为什么。请帮帮我!

【问题讨论】:

    标签: reactjs alt


    【解决方案1】:

    this.generateActions 没有做任何事情...你应该使用alt.generateActions(...)

    此外,像这样自行提取 addUserSuccess 和 addUserFail 操作并没有什么坏处:

    import Api from '../services/QuestionApi';
    
    class QuestionActions {
        constructor() {
            // put auto generate actions here
        }
    
        getQuestions() {
            Api.getQuestions().then((result) => {
                this.getQuestionsSuccess(result);
            });
            return true;
        }
    
        getQuestionsSuccess(data) {
            return data;
        }
    
        createQuestion(question,state) {
            Api.createQuestion(question).then((result) => {
                this.createQuestionSuccess(result);
            });
            return true;
        }
    
        createQuestionSuccess(data) {
            this.getQuestions();
            return data;
        }
    
    
    }
    export default (alt.createActions(QuestionActions));
    

    这将允许您在需要时执行操作中的任何其他功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-21
      • 2016-04-27
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多