Vue3-47-Pinia-修改全局状态变量值的方式

说明

修改全局状态变量的值,是一个比较常规而且常见的操作。

本文就介绍四种常见的操作。

由于Option Store 和Setup Store 在修改的时候略有不同,所以本文也会将不同点体现一下。

全局状态变量的定义

包含了 Option Store 和Setup Store 两种定义方式;

在下面的修改操作中会根据这两种不同的定义来分别阐述。

ts 复制代码
// 导入 defineStore API
import { defineStore } from 'pinia'

// 导入 reactive 依赖
import { reactive } from 'vue'

// 定义全局状态方式一 : option store
export const useClassStore = defineStore('classinfo',{

    state: () => ({
        name:'快乐篮球二班',
        studentNum:30
    }),

    actions:{
      // 用来更新状态的值
      updateName(){
        this.name = '使用actions修改的name'
      }
    }


})

// 定义全局状态方式二 : setup store
export const useStudentStore = defineStore('studentinfo',() => {

    // 响应式状态
    const student =  reactive({
        name : '小明',
        age:12,
        className:'快乐足球一班'
    })


    // 直接定义一个方法进行数据属性的修改
    const updateName = (nameP:string)=>{
        student.name = nameP
    }


    return { student,updateName }

})

方式一 : 直接修改

直接修改 : 就是直接读取对象进行变量值的替换。

Option Store

ts 复制代码
	// 导入全局状态变量的定义
    import  { useClassStore }  from './storea'

    // 获取全局状态变量的对象
    const classStore = useClassStore()

    // 方式一 : 直接修改
   classStore.studentNum = 36

Setup Store

ts 复制代码
	// 导入全局状态变量的定义
    import  { useStudentStore }  from './storea'

    // 获取全局状态变量的对象
    const studentStore = useStudentStore()
    
    // 方式一 : 直接修改
    studentStore.student.className = '我也不知道是哪个班的'

方式二 :$patch 方式修改

参数是一个对象的形式

Option Store

直接放入 【参数对象】 : {key:value} 就OK了

ts 复制代码
	// 导入全局状态变量的定义
    import  { useClassStore }  from './storea'

    // 获取全局状态变量的对象
    const classStore = useClassStore()
    
  	// 方式二 : $patch 方法修改
    classStore.$patch({studentNum:20})

Setup Store

由于 状态变量在定义的时候,就是一个响应式对象,所以需要把整个的对象都放进去才OK。

ts 复制代码
	// 导入全局状态变量的定义
    import  { useStudentStore }  from './storea'

    // 获取全局状态变量的对象
    const studentStore = useStudentStore()
    
    // 方式二 : $patch 方法修改
    studentStore.$patch({student:{
            name: studentStore.student.name,
            age: studentStore.student.age,
            className:'又换了一个班级'
    }})

方式三 : $patch 带参数的方式修改

参数是 函数的形式,且函数的参数就是 原state对象

这种方式用起来比【方式二】要更加灵活。

Option Store

ts 复制代码
	// 导入全局状态变量的定义
    import  { useClassStore }  from './storea'

    // 获取全局状态变量的对象
    const classStore = useClassStore()
    
   // 方式三 :$patch + 函数参数的方法修改 : 函数的参数就是 状态对象
   classStore.$patch((state)=>{
           console.log(state)
           state.studentNum = 100
   })

Setup Store

ts 复制代码
	// 导入全局状态变量的定义
    import  { useStudentStore }  from './storea'

    // 获取全局状态变量的对象
    const studentStore = useStudentStore()
    
    // 方式三 :$patch + 函数参数的方法修改 : 函数的参数就是 状态对象
    studentStore.$patch((state)=>{
         console.log(state)
         state.student.className = '超级无敌快乐踢足球的班'
    })

方式四 :actions方法的方式进行修改

这种方式比较好理解,就是通过调用已经定义好的方法的方式来进行变量值的修改。

也比较推荐使用这一种方式。

Option Store

ts 复制代码
	// 导入全局状态变量的定义
    import  { useClassStore }  from './storea'

    // 获取全局状态变量的对象
    const classStore = useClassStore()

 	// 方式四 :actions 方法的方式进行数据的修改
    classStore.updateName()

Setup Store

ts 复制代码
	// 导入全局状态变量的定义
    import  { useStudentStore }  from './storea'

    // 获取全局状态变量的对象
    const studentStore = useStudentStore()

    // 方式四 :actions 方法的方式进行数据的修改
    studentStore.updateName('小红')
相关推荐
程序员大阳3 天前
副队长Vue3教程(38)--【练手】基于Vue路由的后台权限演示项目
vue3·路由·登录·项目·权限·后台系统·嵌套路由
程序员大阳3 天前
副队长Vue3教程 (23)--组件通信:defineExpose暴露属性与方法
vue3·组件通信·difineexpose·暴露属性·暴露方法
程序员大阳3 天前
副队长Vue3教程(33)--路由懒加载
vue.js·性能优化·vue3·路由·懒加载·速度慢
程序员大阳4 天前
副队长Vue3教程(35)-- 命名路由、嵌套路由、路由元信息
meta·vue3·嵌套路由·命名路由·路由元信息
程序员大阳6 天前
副队长 Vue3 教程 (22)--【练手】封装通用分页组件
vue3·组件·分页·封装
程序员大阳6 天前
副队长Vue3教程(31)--【练手】封装useLocalStorage、简易useFetch
vue3·封装·composables·usefetch·组合式函数·uselocalstorage
程序员大阳6 天前
副队长 Vue3 教程(20)--组件defineEmits子传父
vue3·组件·emit·defineemits·子传父
程序员大阳6 天前
副队长Vue3教程(27)--插槽slot:默认插槽与具名插槽
vue3·slot·插槽·具名插槽·默认插槽
程序员大阳7 天前
副队长Vue3教程(30)--组合式函数composables
vue3·实例·composables·组合式函数
程序员大阳7 天前
副队长Vue3教程(25)--组件的生命周期钩子
vue3·生命周期·组件·钩子·onmounted·onummounted·onupdated