我在一些动态生成的 DOM 元素上使用 HTMX 属性。如文档中所述,除非您致电 ,否则HTMX 无法工作htmx.process()。
当我尝试调用它时,我正确地得到了错误:
未捕获的引用错误:htmx 未定义
知道如何导入这个 htmx 变量吗?不知道文档中的示例如何工作。
谢谢!
非模块版本htmx.org定义了全局,但您正在使用模块(通过import)。模块的一半目的是消除全局变量,因此它的模块版本不会创建全局变量,而是提供导出。
在已删除的评论中,您说实际导入是import "htmx.org"(与import htmx.org仍然存在的评论中不同)。在这种情况下,您可能需要其中之一:
// Importing the default export
import htmx from "htmx.org";
// Or importing the module namespace object
import * as htmx from "htmx.org";
// Or importing a named export, but looking at the file I doubt you want this one
import { htmx } from "htmx.org";
Run Code Online (Sandbox Code Playgroud)
最有可能的是,您想要第一个。
您可以在进行调用的模块中执行此操作htmx.process()。