在Axios请求中调用函数(然后)[Vuejs]

问题描述 投票:0回答:1

我试图在Axios请求中调用show函数,My Axios请求在另一个函数内部,如下所示:

我在Myfunction内的Axios请求:

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
javascript vue.js vuejs2 axios
1个回答
1
投票

一个非常简单的方法是这样做:

app.show = function( workspaceInfo ) { // notice the camel case ;) 
    alert( 'I am here!' );
}

然后将其绑定到应用程序,如下所示:

app.show = app.show.bind( this ); // this is something we do a lot in React

最后,您可以使用它:

app.show( response );

现在,请记住,在实际调用函数之前,您已完成所有设置。

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