ecmascript-6 - 尝试对未通过 npm 安装的库使用 ES2015 模块系统
问题描述
因此,我有几个用于自定义 Web sdk 的依赖库,我正在尝试启动并运行它们。
这些库位于我调用的根文件夹中modules/
,这就是我将它们包含在 HTML 文件中的方式:
<script type="module" src="modules/xm/js-es6/xmsdk-es6.js"></script>
这就是我将它导入index.js
文件的方式:
import { XmSdk } from './modules/xm/js-es6/xmsdk-es6';
为此我得到错误:Cannot use import statement outside a module
.
我认为添加标签就type="module"
足以<script>
解决这个问题,但坦率地说,我从未使用过未node_modules/
安装的 ES2015 模块系统。
下面是index.html
我在index.js
下面的脚本标记中加载文件的文件:
<html>
<head>
<link rel="stylesheet" type="text/css" href="modules/xm/css/xmui.css">
<link rel="stylesheet" type="text/css" href="app.css">
<!-- SDK's CORE API module -->
<!-- <script type="module" src="modules/xm/js-es6/xmsdk-es6.js"></script> -->
<!-- SDK's Default UI Handler module -->
<!-- <script type="module" src="modules/xm/js-es6/xmui-es6.js"></script> -->
<script src="js/ext/require.js"></script>
<script>
requirejs.config({
baseUrl: '',
paths: {
XmSdk: '/xmwl/xm/js-es6/xmsdk-es6',
XmUIHandler: '/xmwl/xm/js-es6/xmui-es6'
}
});
</script>
<!-- SDK's CORE API module -->
<!-- <script type="module" src="modules/xm/js/xmsdk.js"></script> -->
<!-- SDK's Default UI Handler module -->
<!-- <script type="module" src="modules/xm/js/xmui.js"></script> -->
</head>
<body>
<div>
<label for="username">Username:</label>
<input id="username" type="text"/><br>
<!-- <label for="password">Password:</label>
<input id="password" type="text"/><br> -->
<button id="loginButton" type="button">Login</button>
<button id="logoutButton" type="button">Logout</button>
<br>
<div class="transmitContainer"></div>
</div>
<form id="resumeAuthForm" style="display:none" method="post" action="logout.html">
<input type="hidden" id="resumeAuthFormToken" name="xm_auth_token"/>
<input type="hidden" id="resumeAuthFormUsername" name="user_name"/>
</form>
<!-- <script>
require(['XmSdk', 'XmUIHandler'], (xmsdk, xmui) => {
});
</script> -->
<script src="index.js"></script>
</body>
</html>
解决方案
仅使用元素将入口点加载到程序中。<script>
用于type="module"
将其标记为 ES6 模块(只有模块可以导入其他模块):
<script src="index.js" type="module"></script>
不要<script>
使用元素加载其他模块。
<script type="module" src="modules/xm/js-es6/xmsdk-es6.js"></script>
被替换为 import { XmSdk } from './modules/xm/js-es6/xmsdk-es6';
推荐阅读
- monogame - monogame.extended.entities 命名空间中没有世界`
- r - 在插入符号中绘制 ctree 方法决策树,删除下面不需要的条形图
- javascript - 按字段值宽度获取和过滤数组中的图像
- c++ - 使用逗号运算符和可变参数模板参数包的折叠表达式
- python - 从 Tkinter 文本字段中获取列表,检查重复项并删除任何
- python - 从选定数量的行中查找局部最大值
- angular - 函数中多个可观察调用的返回值
- swift - 如何在 Swift 中避免 Firebase noSQL db 中的循环依赖?
- excel - 显示最近使用的单元格的宏
- javascript - 为什么 App Script 为数组中的数字添加小数?