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)

仓库创建

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容