// stores/counter.js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => {
return { count: 0 }
},
// 다음과 같이 정의할 수도 있음:
// state: () => ({ count: 0 })
actions: {
increment() {
this.count++
},
},
})
<script setup>
import { useCounterStore } from '@/stores/counter'
const counter = useCounterStore()
counter.count++
counter.$patch({ count: counter.count + 1 })
// 또는 actions 사용
counter.increment()
</script>
<template>
<!-- 스토어에서 직접 상태에 액세스 -->
<div>현재 카운트: {{ counter.count }}</div>
</template>
Composition API란?
옵션을 선언하는 대신 import한 함수를 사용하여 vue 컴포넌트를 작성할 수 있는 API 세트이다.
export const useCounterStore = defineStore('counter', () => {
const count = ref(0)
function increment() {
count.value++
}
return { count, increment }
})
이번 기회에 pinia에 대해 알게 되었네요. 감사합니다.