Pinia in Vue3

Pinia

State management
state | getter | action

Basic Create and Usage

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import { defineStore } from 'pinia'

    export const useAbcStore = defineStore('abc', {
    state: () => ({ count: 0 }),
    getters: {
    double: (state) => state.count * 2,
    },
    actions: {
    increment() {
    this.count++
    },
    },
    })
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import { defineStore } from 'pinia'
    import { ref,computed } from 'vue'

    export const useAbcStore = defineStore('abc', {
    const count = ref(0);
    const double = computed(() => {
    return count*2
    });
    function increment() {
    count.value ++
    };
    return {
    count,double,increment
    }
    })
    Usage
    1
    2
    3
    4
    5
    <script setup>
    import { useAbcStore } from '@/stores/abc'

    const count = useAbcStore()
    </script>

state seems to data

getter seems to computed

action seems to methods


本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2023/10/Vue/Pinia/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!