JavaScript / HTML:如何导入本地JSON /理解模块访问
我是JavaScript / HTML的新手,并且有一些问题可以理解导入 /模块功能。我需要将JSON文件加载到JavaScript中。
我能找到的最好的方法是通过导入,例如:
import myJason from './dummy.json' assert {type: 'json'};
尝试时,我会遇到以下错误:
Cannot use import statement outside a module
因此我将其放入模块(二手标头和车身)中。 但是我无法访问模块功能。
Google无法帮助我,可以请有人解释如何正确执行此操作。
注意:HTML和JSON文件是本地文件(无法通过Web服务器访问)
这是(减少)代码:
<html lang="en">
<head>
<script type="module">
import myJason from './dummy.json' assert {type: 'json'};
function dummy(){
//evaluate JSON
return result();
}
window.dummy = dummy;
export{dummy}
</script>
</head>
<body>
<script>
// tried (and other things)
// dummy();
// window.dummy();
</script>
</body>
</html>
I am new to javascript / html and have some problems to understand the import / module functionality. I need to load a json file into javascript.
The best way I could find is via import, like:
import myJason from './dummy.json' assert {type: 'json'};
When trying it, I am getting following error:
Cannot use import statement outside a module
So I placed it into a module (tried header and body).
But I am not able to access the module functions.
Google could not help me, could please somebody explain how to do it properly.
Note: HTML and JSON files are local files (not accessed via Webserver)
Here is the (reduced) code:
<html lang="en">
<head>
<script type="module">
import myJason from './dummy.json' assert {type: 'json'};
function dummy(){
//evaluate JSON
return result();
}
window.dummy = dummy;
export{dummy}
</script>
</head>
<body>
<script>
// tried (and other things)
// dummy();
// window.dummy();
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用XML httprequest
,甚至更好地fetch api检查在这里
Try using XML HttpRequest
or even better the fetch api check that here