我有一个 vue 程序,它使用服务从 API 调用获取产品。
getProducts() {
axios.get("https://jsonplaceholder.typicode.com/posts").then(function (response) {
return response;
}).catch(function(error){
console.log(error);
})
}
在我需要调用它的 vue 页面中,我在created() 钩子(Hook)中进行调用,如下所示:
created() {
//this returns a promise
productservices.getProducts().then(response =>{
this.products = response.data;
}).catch(error => console.log(error))
.finally(() =>{
consol.log("All done!");
})
},
我可以毫无问题地调用该函数,但它正在运行 .then() 并返回 Undefined。为什么会发生这种情况?我该如何修复?
最佳答案
getProducts() {
return axios.get("https://jsonplaceholder.typicode.com/posts")
}
试试这个。
关于javascript - 在解决 promise 之前,函数正在重新调整未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075059/