chenyc
2023-05-28 aae6cb63fac8f2956615481382c101cfd2e7d45c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * pinia
 * 文档指引 https://pinia.vuejs.org/introduction.html
 * **/
 
import { defineStore } from 'pinia'
import { ref } from 'vue'
// useStore could be anything like useUser, useCart
// the first argument is a unique id of the store across your application
// 使用示例一:函数式定义【个人推荐】
export const useCounterStore = defineStore('counter', () => {
    const count = ref(0)
    function increment() {
        count.value++
    }
 
    return { count, increment }
})
 
// 使用示例二:基本使用
export const useCounterStore2 = defineStore('counter2', {
    state: () => {
        return {
            count: 0
        }
    },
    actions: {
        increment() {
            this.count++
        }
    }
})
 
/*
// 调用示例
import { useCounterStore } from '@/stores/counter'
 
export default {
    setup() {
        const counter = useCounterStore()
 
        counter.count++
        // with autocompletion ✨
        counter.$patch({ count: counter.count + 1 })
        // or using an action instead【个人推荐】
        counter.increment()
    },
}*/