在解析promise之前,函数重定义未定义

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

我有一个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。为什么会这样?我该如何解决?

javascript node.js angular vue.js
1个回答
2
投票
getProducts() {
    return axios.get("https://jsonplaceholder.typicode.com/posts")
}

试试这个。

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