我试图在 Axios 请求中调用 show
函数,我的 Axios 请求在另一个函数中调用,如下所示:
我的 Axios 请求在 Myfunction
中:
axios({
method: "Get",
timeout: 3000,
headers: {
..................
},
url: "https://XXXXXX/"
})
.then( function(response) {
console.log(response);
//Call function
app.show.bind(response);
})
.catch(function(error) {
console.log(error);
});
函数 show
位于方法部分:
show (workspace_info) {
alert("I am here");
},
但我收到一条错误消息:
TypeError: Cannot read property 'bind' of undefined
最佳答案
一个非常简单的方法是这样做:
app.show = function( workspaceInfo ) { // notice the camel case ;)
alert( 'I am here!' );
}
然后将其绑定(bind)到应用程序,如下所示:
app.show = app.show.bind( this ); // this is something we do a lot in React
最后,您可以像这样使用它:
app.show( response );
现在,请记住在实际调用该函数之前完成所有设置。
关于javascript - Axios请求内部调用函数(then)[Vuejs],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271179/