12345678910111213141516171819202122232425 |
- import { defineStore } from "pinia";
- export const useCounterStore = defineStore("counter", {
- state: () => ({
- count: 0
- }),
- actions: {
- increment() {
- this.count++
- }
- }
- })
- // 使用
- /*
- <script setup>
- import { useCounterStore } from '~/stores/counter'
- const counterStore = useCounterStore()
- </script>
- <template>
- <div>
- <p>Count: {{ counterStore.count }}</p>
- <button @click="counterStore.increment">Increment</button>
- </div>
- </template>
- */
|