【问题标题】:Vue.js calling an async function from external js fileVue.js 从外部 js 文件调用异步函数
【发布时间】:2020-12-19 03:21:55
【问题描述】:

我正在尝试创建一个 .js 文件,其中有几个异步调用。 我设置了文件,但是当我调用我的方法时没有得到任何结果。 从 .js 文件调用这对我来说是全新的,所以不确定我做错了什么。

这是我的inventory.js文件从“axios”导入axios;

let getInventories = async () => {
  const result = await axios
    .get("/inventories")
    .catch((error) => console.log(error));
  // this.inventoryArray = result.data;
}

export {getInventories}

这是来自我的 Inventory.vue 文件的调用

import axios from "axios";
import { bus } from "../app";
import {getInventories} from './inventory';
export default {
  mounted() {
    let temp =  getInventories();
    debugger;
  },
}

temp 没有返回任何东西。我从getInventories 中添加了等待,但出现错误

【问题讨论】:

    标签: javascript vue.js vuejs2 axios vue-component


    【解决方案1】:

    您缺少返回结果:

    let getInventories = async () => {
       try{
        const result = await axios
        .get("/inventories")
        return result.data;
       }   catch(error){
       console.log(error);
       return null;
       };
     
    }
    
    export {getInventories}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多