hx-replace-url

hx-replace-url 屬性允許您替換瀏覽器歷史紀錄中當前的 URL。

此屬性的可能值為:

  1. true,這會替換瀏覽器導覽列中獲取的 URL。
  2. false,如果原本因為繼承而會被替換,則會停用替換獲取的 URL。
  3. 要替換到網址列的 URL。這可以是相對或絕對路徑,如同 history.replaceState()

以下是一個範例:

<div hx-get="/account" hx-replace-url="true">
  Go to My Account
</div>

這將導致 htmx 快照目前的 DOM 到 localStorage,並將瀏覽器網址列中的 URL 替換為 `/account`。

另一個範例:

<div hx-get="/account" hx-replace-url="/account/home">
  Go to My Account
</div>

這將會把瀏覽器網址列中的 URL 替換為 `/account/home`。

注意事項