导出默认异步函数在另一个 js 导入期间没有执行整个函数

Export default async function haven't execute the whole function during import in another js

我想通过在http-common.js中触发内部API调用来定义axios baseURL,但是当我从http-common.js导入模块时,它无法获取axios目的。我发现即使是异步函数也没有 运行 ...有人知道吗?非常感谢。

在 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");
  }

.....

因为您正在导出一个 async 方法,您需要等待它。

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

当然,您需要从 async 上下文中调用它。