![見出し画像](https://assets.st-note.com/production/uploads/images/125030818/rectangle_large_type_2_0de1fefdacac687c14577cf401ecb30c.png?width=800)
CompositionAPIで書かれたPiniaStoreの構文、VueDevtoolsではどのように表示されるのか検証してみた
先日DevtoolsでPiniaの欄を開いた際に疑問に思った。
「state,gettersが表示されているけど、CompositionAPIのsetupで書かれたStoreの構文、Devtoolsではどのように表示されるの?」
![](https://assets.st-note.com/img/1702991300746-HQrQvGzGOl.jpg?width=800)
前提
CompositionAPIのsetupを用いてStoreを記述する際、以下のように記述する
export const useCounterStore = defineStore('counter', () => {
const count = ref(0) //stateと同じ
const name = ref('Eduardo')
const doubleCount = computed(() => count.value * 2) //gettersと同じ
function increment() { //actionsと同じ
count.value++
}
return { count, name, doubleCount, increment }
})
検証結果
以下Storeに記述し、Devtoolsを開く
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
interface CounterState {
count: number
}
export const useCounterStore = defineStore('counter', () => {
const state = ref<CounterState>({
count: 0
})
const doubleCount = computed(() => state.value.count * 2)
function increment() {
state.value.count++
}
return { state, doubleCount, increment }
})
![](https://assets.st-note.com/img/1702991196119-iR2G6BAUlB.png)
stateとgettersに反映されている..!
新たな発見だったが、
・OptionsAPIでの構文前提知識がないと理解ができない
・constで定義した内容と相違がある為、どこがどの処理で出力されているのか慣れるまで見比べる必要がある
CompositionAPIのsetup用にアップデートされるのを切に願う
(関係ないけどPiniaのキャラクターかわいいな)
この記事が気に入ったらサポートをしてみませんか?