我有一个vue程序,它使用服务从API调用中获取产品。
getProducts() {
axios.get("https://jsonplaceholder.typicode.com/posts").then(function (response) {
return response;
}).catch(function(error){
console.log(error);
})
}
在我需要调用的vue页面中,我在created()钩子中进行调用,如下所示:
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")
}
试试这个。