【问题标题】:How to import a js file module in ASP.NET CORE MVC?如何在 ASP.NET CORE MVC 中导入 js 文件模块?
【发布时间】:2021-03-15 10:57:48
【问题描述】:

我刚刚在 wwwroot/js/base.js 上创建了一个 js 模块文件:

export const elements = {
...somthing
};

当我尝试从 wwwroot/js/site.js 导入时,比如

import {elements} from './base';

我得到这个错误:

Uncaught SyntaxError: Cannot use import statement outside a module

我错过了什么?

【问题讨论】:

    标签: javascript asp.net-core-mvc


    【解决方案1】:

    如果要导入js文件模块,需要这样添加(必须在script标签中使用type="module"):

    <script type="module" src="~/js/site.js"></script>
    

    site.js默认导入Views/Shared/_Layout.cshtml,可以查看Views/Shared/_Layout.cshtml

    <script src="~/js/site.js" asp-append-version="true"></script>
    

    <script type="module" src="~/js/site.js" asp-append-version="true"></script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2018-08-13
      • 2021-08-01
      相关资源
      最近更新 更多