Created
October 30, 2023 13:53
-
-
Save nexpr/9d3ebd0ab580974038840ccb6102077e to your computer and use it in GitHub Desktop.
Vue3 + htm example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset="utf-8" /> | |
<script type="module"> | |
import { createApp, ref, h } from "https://unpkg.com/[email protected]/dist/vue.esm-browser.js" | |
import htm from "https://cdn.jsdelivr.net/npm/[email protected]/mini/index.module.js" | |
const html = htm.bind( | |
(type, props, ...children) => | |
h(type, props, typeof type === "string" ? children : () => children) | |
) | |
const Component = { | |
props: ["foo"], | |
setup(props, { slots }) { | |
return () => html` | |
<div> | |
<div>foo: ${props.foo}</div> | |
<div>slot: ${slots.default()}</div> | |
</div> | |
` | |
}, | |
} | |
const App = { | |
components: { | |
Component, | |
}, | |
setup() { | |
const count = ref(1000) | |
const text = ref("") | |
return () => html` | |
<div> | |
<button onClick=${() => count.value++}>${count.value}</button> | |
${[1, 2, 3].map(n => { | |
return html` | |
<div> | |
<span>${n}</span> | |
<input | |
value=${text.value} | |
onInput=${(event) => text.value = event.target.value} | |
/> | |
</div> | |
` | |
})} | |
<${Component} foo=${count.value}>${text.value}</> | |
</div> | |
` | |
}, | |
} | |
createApp({ | |
components: { | |
App, | |
}, | |
setup() { | |
return () => html`<${App} />` | |
}, | |
}).mount("#root") | |
</script> | |
<div id="root"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment