本指南的目的是提供从 htmx 1.x 迁移到 htmx 2.x 的说明。我们非常重视向后兼容性,因此在大多数情况下,这些迁移应该只需要很少的工作,甚至不需要工作。
/dist/htmx.esm.js
/dist/htmx.amd.js
/dist/htmx.cjs.js
/dist/htmx.js
文件仍然可以被浏览器加载hx-ws
和 hx-sse
属性,请升级到扩展版本htmx.config.scrollBehavior
还原为 'smooth'
DELETE
请求使用表单编码的正文而不是参数,请将 htmx.config.methodsThatUseUrlParams
还原为 ["get"]
(这有点奇怪,但根据规范,DELETE
应该像 GET
一样使用请求参数)。htmx.config.selfRequestsOnly
还原为 false
hx-on
属性转换为其 hx-on:
等价物 <button hx-get="/info" hx-on="htmx:beforeRequest: alert('Making a request!')
htmx:afterRequest: alert('Done making a request!')">
Get Info!
</button>
变成 <button hx-get="/info" hx-on:htmx:before-request="alert('Making a request!')"
hx-on:htmx:after-request="alert('Done making a request!')">
Get Info!
</button>
Note that you must use the kebab-case of the event name due to the fact that attributes are case-insensitive in HTML.
htmx.makeFragment()
方法现在始终返回 DocumentFragment
,而不是 Element
或 DocumentFragment
selectAndSwap
方法,该方法已被删除并替换为 swap
方法,该方法可从内部和公共 htmx API 中使用要使用新方法进行交换,您只需使用
let content = "<div>Hello world</div>"; // this is HTML that will be swapped into target
let target = api.getTarget(child);
let swapSpec = api.getSwapSpecification(child);
api.swap(target, content, swapSpec);
swap
方法文档可在 JS API 参考 中找到
IE 在 htmx 2.0 中不再受支持,但 htmx 1.x 继续支持 IE 并且将在可预见的未来得到支持。
这是官方的 htmx 1.x -> 2.x 升级音乐