Pinia 的常用方法

VUE3 非組合式

import { useTabStore } from "@/pinia/store"
import {mapState,mapActions} from "pinia"

  methods: {
   ...mapActions(useProfileStore,{
      LoginByUsername:"LoginByUsername"
    })  
  },

  computed:{
    ...mapState(
      useTabStore,{
        tabs : 'tabs',
        activePath : 'activePath'
      }
    ),
    systemInfo(){
      return "version 1"
    }
  },

組合式API

// app.ts
import { ref, computed,toRefs } from 'vue'
import { defineStore } from 'pinia'

export const useAppStatStore = defineStore('appstat', () => {

    const viewLg = computed(()=>{
        return winWidth.value > 1280 
    })


    return {
        viewLg 
    }
})
// 應用
import { ref, computed, toRefs } from 'vue'
import { useAppStatStore } from '@/store/app'

const useAppStat = useAppStatStore()
const { viewLg } = toRefs(useAppStat)

倉庫創(chuàng)建

import { defineStore } from 'pinia'
import { ref, computed,toRefs } from 'vue'

export const mainStore = defineStore('mainPinia', () => {

    let loginUser = ref(null) as any ;

    return {
      loginUser
    }
}

Ref:
https://zhuanlan.zhihu.com/p/533233367
持久化:
https://www.jb51.net/article/249783.htm

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容