class test {
constructor() {
console.log("init");
}
}
module.exports = test;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>drawBoard</title>
</head>
<body>
</body>
<script type="text/javascript" src="../build/test.js"></script>
<script type="text/javascript">
console.log(typeof test) // undefined
</script>
</html>
这样直接调用肯定是会失败的。
我看了 webpack 打包后的代码,理解是将 打包了一个匿名函数,然后在匿名函数中返回了 module.exports 的 类 test。
用 es6 写可以用 import,可是用 es5 怎样调用呢?
求解,谢谢。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
webpack的配置里面output.libraryTarget设置为 'umd'