【发布时间】:2021-02-18 16:07:39
【问题描述】:
我有一个 api 调用设置来在我的快速服务器启动时检索我需要的一些数据,我想通过导入来跨多个文件重用这些数据。基本上使它成为一个全球性的,但只能通过导入/导出访问。
更新:我只想让getCreds api 调用一次,因为它们不会更改然后在我的应用程序中引用凭据。
不过,我不确定最好的方法。 :/
file1.js、file2.js 等等……
import creds from './libs/creds.js'
...use creds for something...
libs/creds.js
// not sure how to set creds once and export it
let creds;
const getCreds = async () => {
...api call to get creds, but do not want to call repeatedly...
creds = api.data
}
export { creds };
【问题讨论】:
标签: javascript node.js ecmascript-6