简体   繁体   English

Java 脚本模块(导入/导出)与 Asp.net Core Mvc

[英]Java Script Module(Import/Export) with Asp.net Core Mvc

I'm trying to work with modules, but when I try to import and use the method I receive the error - reference error onLogin is not defined , what can I be doing wrong?我正在尝试使用模块,但是当我尝试导入和使用我收到错误的方法时 - reference error onLogin is not defined ,我做错了什么?

  • LoginController.js登录控制器.js

import Api from "/src/Utils/Api.js";从“/src/Utils/Api.js”导入 Api;

export default async function onLogin() {

try {
    debugger
    document.getElementById("loading").style.visibility = 'visible';

    var usuario = {
        snome: document.getElementById("EdtUsuario").value,
        ssenha: document.getElementById("EdtSenha").value
    };

    let retorno = await Api.post('/Login/login/', usuario);

    console.log(retorno);

} catch (error) {

    document.getElementById("loading").style.visibility = 'hidden';
    console.log(error);

}
}
  • Import in View在视图中导入
<script type="module"> import onLogin from '/src/Controller/LoginController.js'</script>

The Method onLogin is called on event onClick在事件 onClick 上调用方法 onLogin

<button onclick="onLogin()" type="submit" class="botaowidth100">Entrar</button >
  • you can try new feature ImportMap您可以尝试新功能ImportMap
  1. add script import map in _Layout.cshtml在_Layout.cshtml中添加脚本import map
 <script type="importmap">
        {
          "imports": {
            "AjaxGet": "/js/AjaxGet.js",
            "AjaxPostJSON": "/js/AjaxPostJSON.js"
          }
        }
    </script>
  1. import in your script section在你的脚本部分导入
@section Scripts
{
<script type="module">
    import {AjaxGet} from "AjaxGet";

      $('#createBtn').click(function() {
            if (!confirm("Are you sure to create new sem account?"))
                return;
          
            AjaxGet("@Url.Action("Create")", {}, doneFunc);
          })
</script>
}

I think you will run into CORS error if you try to load js files locally due to security reasons.如果出于安全原因尝试在本地加载 js 文件,我认为您会遇到 CORS 错误。 So you need to either not use script as module or serve the module from a server.因此,您需要不使用脚本作为模块或从服务器提供模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM