ES6-从一个文件导入到另一个文件的方法会出错

ES6-Importing method from one file to another gives error

我有一个 API 实用程序文件 React JS,其中我有如下声明的端点列表-

File1.js-
    export const api_endpoints = {
     api1: ()=> '../data/user'
    }

我想在我的另一个文件 (File2.js) 中使用来自 File1.js 的 api1(),因此我像下面这样导入它-

import {api_endpoints} from '../File1';
const myObj = {
  [api_endpoints.api1()]: '../data/user2'
}

这让我在 File2 中出错 -

"cannot read property api1 of undefined"

我不确定为什么 api_endpoints 在 File2.js 中未定义。路径是正确的。谁能帮我理解一下。

您可能刚刚复制错了,但您导入了 {api1_endpoints},但要求 api_endpoints

您似乎是从错误的路径导入的。试过上面的例子,它对我来说工作正常..

https://codesandbox.io/s/objective-meninsky-7gbch?file=/src/File2.js