如何从另一个JavaScript文件获取数据

发布于 2025-01-21 13:32:11 字数 419 浏览 1 评论 0原文

我正在尝试使用fetch API和异步等待在另一个文件中获取数组,并将其在单独的JS文件中使用。

例如:

data.js app.js

我在app.js中工作,并且想从data.js

data.js中使用数据(在数组中)看起来像这样:

//data.js
const data = ['steve','jim','julie']
//app.js
const res = fetch('./data.js')
const data = res.data

我似乎无法从data.js读取数据变量。 JS。

I am trying to use the fetch API and async await to fetch an array in another file and use it in a separate js file.

For example:

data.js
app.js

I am working within app.js and would like to use the data (in an array) from data.js

data.js looks like this:

//data.js
const data = ['steve','jim','julie']
//app.js
const res = fetch('./data.js')
const data = res.data

I seem to be unable to read the data variable from data.js into app.js.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2025-01-28 13:32:11

看起来您想进行javascript导入

// module "my-module.js"

export default function cube(x) {
  return x * x * x;
}


// another file.js

import cube from './my-module.js'
console.log(cube(3)); // 27

https:/https:/ /developer.mozilla.org/en-us/docs/web/javascript/reference/reference/statements/export

It looks like you want to do javascript import

// module "my-module.js"

export default function cube(x) {
  return x * x * x;
}


// another file.js

import cube from './my-module.js'
console.log(cube(3)); // 27

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

赤濁 2025-01-28 13:32:11

要导出数据,请使用导出:

export const data = ['steve','jim','julie'];

然后将其导入主文件并显示:

const your_export = require('./modules');
let {  data } = your_export
console.log(data)

您可以阅读有关它的更多信息在这里

To export the data, use export:

export const data = ['steve','jim','julie'];

then import it in your main file and display it:

const your_export = require('./modules');
let {  data } = your_export
console.log(data)

You can read more about it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文