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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 基础类问题 h5的新特性 html5备注只有一种 DOCTYPE ⽂件类型声明(统 一标准)<!DOCTYPE h...
    h2coder阅读 381评论 1 1
  • 前端经典面试题: 1、(前端面试题)https://zhuanlan.zhihu.com/p/84212558?f...
    who_are_you_阅读 540评论 0 2
  • html HTML5新特性,语义化https://www.cnblogs.com/vicky1018/p/7705...
    _旁观者_阅读 788评论 0 0
  • 闭包 1)闭包定义 闭包:对于一个嵌套定义的函数(函数中定义函数),外部函数的返回值是内部函数,而在内部函数中又引...
    马尔代夫Maldives阅读 1,883评论 0 0
  • 1、sklearn StandardScaler与“ with_std = False或True”之间的差异和“ ...
    无量儿阅读 829评论 0 0

友情链接更多精彩内容