那么来自axios内部的函数调用呢?(Function call from inside axios then?)

编程入门 行业动态 更新时间:2024-10-26 18:20:29
那么来自axios内部的函数调用呢?(Function call from inside axios then?)

如何从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

更多推荐

本文发布于:2023-07-19 21:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   axios   call   Function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!