Vue counter (Composition API - not working)
by nolleto
HTML
<script src="https://unpkg.com/vue@next"></script>
<body>
<div id="app">
<counter-component />
</div>
</body>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
h2 {
margin-bottom: 8px;
}
JavaScript
const { createApp } = Vue
const app = createApp({});
const useCounter = () => {
let counter = 3
const increment = () => {
counter = counter + 1
}
const decrement = () => {
counter = counter - 1
}
return { counter, increment, decrement }
}
app.component('counter-component', {
setup() {
const { counter, increment, decrement } = useCounter()
return { counter, increment, decrement }
},
template: `
<div>
<h2>Counter: {{ counter }}</h2>
<button @click="increment">Increment</button>
<button @click="decrement">Decrement</button>
</div>
`
})
app.mount("#app");