[英]How to import a js file module in ASP.NET CORE MVC?
我剛剛在 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
我錯過了什么?
如果你想導入一個js文件模塊,你需要像這樣添加(你必須在腳本標簽中使用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>
有時您需要在添加 type="module" 后更新文件的 JS 版本,如下所示:
<script type="module" src="~/js/site.js?v=101" asp-append-version="true"></script>
因為您的瀏覽器可以緩存已經存在的腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.