繁体   English   中英

Javascript 库没有在脚本标签中使用导入

[英]Javascript library without using an import in the script tag

I try to create a library using typescript and I would like to use a static method that allows me to render some HTML content that is returned to me by an api rest call based on the parameters I pass to that function now there is a way to使用架构(例如模式存储库)创建它,编译它,然后按照下面的代码使用它(当然是 js 格式)?

<div id="test"></div>

<script scr="../myLybrary.js"></script>
<script type="module">

    MyLibrary.render({
        param: "test",
        param1: "test",
        param2: "test"
    }, "test")

</script>

目前要创建这种情况,我必须在脚本中使用导入,其中对 static 方法的调用以这种方式驻留:

import MyLibrary from './myLybrary.js'

有什么办法可以避免这最后一步?

感谢任何想帮助我的人。

目前要创建这种情况,我必须在脚本中使用导入,其中对 static 方法的调用以这种方式驻留:

 import MyLibrary from './myLybrary.js'

这通常是正确的做法。 您只需删除第一个script标签,因为使用它您最终会加载您的库两次。 所以这给我们留下了:

<div id="test"></div>

<!-- Note: No `script` tag for `myLybrary.js` here. -->
<script type="module">
    import MyLibrary from "./myLybrary.js";

    MyLibrary.render({
        param: "test",
        param1: "test",
        param2: "test"
    }, "test");

</script>

加载模块会运行模块中的顶级代码,因此如果myLybrary.js具有加载某些资源的顶级代码,则该代码将在处理import声明时运行。

暂无
暂无

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

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