如何从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函数没有执行!! 任何想法
How to call a function from inside axios then below is my code which is not working
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 function is not executing !! any idea
最满意答案
您的代码无法正常工作,因为您的代码将使用您当前的实现未定义。
你能试试吗?
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的概念。 这不是应该分派行动的方式
Your code is not working working because your this would be undefined with the current implementation you have.
Can you try 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") }Now i see above wont work too, even though you change it to self. You are trying to using dispatch. In dispatch you need to pass an action. . Reducers take state and action as parameters and they update state based on what action is passed.
Now an action may return an object or a function. Please go through concepts of redux once. This is not the way an action should be dispatched
更多推荐
发布评论