admin管理员组文章数量:1394585
I just created a js module file on wwwroot/js/base.js:
export const elements = {
...somthing
};
When I try to import it from wwwroot/js/site.js, such as
import {elements} from './base';
I geth this error:
Uncaught SyntaxError: Cannot use import statement outside a module
What am I missing?
I just created a js module file on wwwroot/js/base.js:
export const elements = {
...somthing
};
When I try to import it from wwwroot/js/site.js, such as
import {elements} from './base';
I geth this error:
Uncaught SyntaxError: Cannot use import statement outside a module
What am I missing?
Share Improve this question asked Mar 12, 2021 at 16:52 חיים חדדחיים חדד 5221 gold badge5 silver badges19 bronze badges2 Answers
Reset to default 4If you want to import a js file module,you need to add like this(You must use type="module"
in script tag):
<script type="module" src="~/js/site.js"></script>
site.js is imported in Views/Shared/_Layout.cshtml
by default.You can check Views/Shared/_Layout.cshtml
and change
<script src="~/js/site.js" asp-append-version="true"></script>
to
<script type="module" src="~/js/site.js" asp-append-version="true"></script>
Sometimes you need to update the JS version of the file after you added the type="module" as below:
<script type="module" src="~/js/site.js?v=101" asp-append-version="true"></script>
Because your browser can cache already existing scripts.
本文标签: javascriptHow to import a js file module in ASPNET CORE MVCStack Overflow
版权声明:本文标题:javascript - How to import a js file module in ASP.NET CORE MVC? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744097112a2590482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论