导出默认的async函数在另一个js中导入时没有执行整个函数。

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

我想在http-common.js中通过触发内部API调用来定义axios baseURL,但是当我从http-common.js中导入模块时,却无法获得axios对象。我发现即使是async函数也没有运行。有谁知道这个问题吗?非常感谢。

在http-common.js文件中

export default async function getAPIEndPoint() {
  var endpoint = await axios.get("http://localhost:8082/getAPIEndPoint");

  BASE_API = endpoint.data;
  var axoisInstance = axios.create({
     baseURL: BASE_API,
     headers: {
      "Content-type": "application/json"
     }
  });

  return axoisInstance;
};

在ProductService.js文件中

import http from "../http-common";

class ProductDataService {
  getAll() {
    return http.get("/getAllRegisteredProducts");
  }

.....

node.js vue.js asynchronous export default
1个回答
0
投票

因为你导出的是一个 async 方法,你需要等待它。

const axiosInstance = await http
return axiosInstance.get("/getAllRegisteredProducts");

当然,你需要从一个 async 语境。

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