[ Home ]
back

htmx + Vanilla.js

id: 124, 2024-08-18

概要:

htmx + Vanilla.js 的な構成になります。

  • bundler は、Viteを使用する例です。

[ 作成日: 2024/08/17 ]


構成

  • htmx
  • Vite:5.2
  • typescript: 5.4
  • express

関連


作成したコード


  • src/pages/App.tsx
  • SSR表示の例
import renderLayout from "./renderLayout";
//
export default function Page(props: any) { 
  //
  const htm = `
  <h1 className="text-4xl font-bold">hello</h1>
  `;
  return renderLayout({children: htm, title: "Home"});
}

  • src/pages/Htmx3.ts
  • hx-postの例、API連携します。
   <form
    hx-post="/api/test/test"
    hx-trigger="submit"
    hx-target="#h2"
    hx-on="htmx:afterRequest: alert("OK, post send")"
    >
      <input type="text" name="name" />
      <button type="submit">Add</button>
    </form>
    <hr />
    <h3 id="h2">ここに表示</h3>