那么来自axios内部的函数调用呢?

问题描述 投票:4回答:2

如何从axios内部调用函数然后在下面是我的代码,这是无效的

handleClick(userid){
  axios.get(
    "http://localhost:4000/user/"+userid+"/items.json")
    .then(function (response) {
      dispatch(this.buildhtml.bind(response))
    })
}


  buildhtml(response){
  console.log("m called")
  }

buildhtml函数没有执行!!任何的想法

reactjs axios
2个回答
5
投票

您的代码无法正常工作,因为您的this将使用您当前的实现未定义。

你能试试吗?

handleClick(userid){
  var self=this;
  axios.get(
    "http://localhost:4000/user/"+userid+"/items.json")
    .then(function (response) {
      self.buildhtml.bind(response) // would work
      dispatch(self.buildhtml.bind(response)) //wont work
    })
}

  buildhtml(response){
  console.log("m called")
  }

现在我看到上面也不会工作,即使你把它改成自己。您正在尝试使用调度。在调度中,您需要传递一个动作。 。 Reducers将state和action作为参数,并根据传递的操作更新状态。

现在,动作可以返回对象或函数。请仔细阅读redux的概念。这不是应该分派行动的方式


3
投票

当使用Vue.jsaxios时,我也有这个问题但是,这就是我解决它的方法。

let vue = new Vue({
 el:#<element>,
 methods:{

  method1()
  {
 axios.post('<url>',{<data>})
 .then( ( res ) => {
  this.method2(res)  // method call to method2 
  } )
 .catch( ( err ) => { <call> } );     
  },//method1 end

method2(res){
// statements
} 

}
});

请参阅this

© www.soinside.com 2019 - 2024. All rights reserved.