我想在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");
}
.....
因为你导出的是一个 async
方法,你需要等待它。
const axiosInstance = await http
return axiosInstance.get("/getAllRegisteredProducts");
当然,你需要从一个 async
语境。