Setup a simple Pinia store for counter and use it in a component.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
js
// store.js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() { this.count++ }
}
})
// Component.vue
<script setup>
import { useCounterStore } from './store'
const store = useCounterStore()
</script>
<template>
<button @click="store.increment">{{ store.count }}</button>
</template>