如何从另一个JavaScript文件获取数据
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想进行javascript导入
https:/https:/ /developer.mozilla.org/en-us/docs/web/javascript/reference/reference/statements/export
It looks like you want to do javascript import
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
要导出数据,请使用导出:
然后将其导入主文件并显示:
您可以阅读有关它的更多信息在这里
To export the data, use export:
then import it in your main file and display it:
You can read more about it here