gpt4 book ai didi

javascript - 无法从 redom 库导入

转载 作者:行者123 更新时间:2023-11-28 02:36:28 25 4
gpt4 key购买 nike

我正在尝试导入 redom库,但不断收到此错误:

未捕获的 TypeError:无法读取 null 的属性“nodeType”

我的代码如下:

<!DOCTYPE html>
<html>
<head>
<script src='test.js'></script>
<script src="libraries/redom.min.js" type='text/javascript'></script>
<script>
const { el, mount } = redom;
const hello = el('h1', 'Hello World');
mount(document.body, hello);
</script>

</head>
<body>
</body>
</html>

如果我将 const { el, mount } = redom; 更改为 import { el, mount } from 'redom',我会得到 Uncaught SyntaxError: Unexpected token 导入

路径正确,redom.min.js没有坏。

最佳答案

向加载 redom 的脚本添加属性 type="module" 可能会解决问题(如果您使用最新的 Chrome):

<script src="libraries/redom.min.js" type="module"></script>

然而,由于大多数浏览器不支持 import 语句,因此预计会有异常,请参阅引述:

Note: This feature is not implemented in any browsers natively at this time. It is implemented in many transpilers, such as the Traceur Compiler, Babel, Rollup or Webpack.

Source: import - JavaScript | MDN

关于javascript - 无法从 redom 库导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47002638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com