Vue3 Pinia如何修改全局狀態(tài)變量值
說明
修改全局狀態(tài)變量的值,是一個比較常規(guī)而且常見的操作。
本文就介紹四種常見的操作。
由于Option Store
和Setup Store
在修改的時候略有不同,所以本文也會將不同點(diǎn)體現(xiàn)一下。
全局狀態(tài)變量的定義
包含了 Option Store
和Setup Store
兩種定義方式;
在下面的修改操作中會根據(jù)這兩種不同的定義來分別闡述。
// 導(dǎo)入 defineStore API import { defineStore } from 'pinia' // 導(dǎo)入 reactive 依賴 import { reactive } from 'vue' // 定義全局狀態(tài)方式一 : option store export const useClassStore = defineStore('classinfo',{ state: () => ({ name:'快樂籃球二班', studentNum:30 }), actions:{ // 用來更新狀態(tài)的值 updateName(){ this.name = '使用actions修改的name' } } }) // 定義全局狀態(tài)方式二 : setup store export const useStudentStore = defineStore('studentinfo',() => { // 響應(yīng)式狀態(tài) const student = reactive({ name : '小明', age:12, className:'快樂足球一班' }) // 直接定義一個方法進(jìn)行數(shù)據(jù)屬性的修改 const updateName = (nameP:string)=>{ student.name = nameP } return { student,updateName } })
方式一:直接修改
直接修改 : 就是直接讀取對象進(jìn)行變量值的替換。
Option Store
// 導(dǎo)入全局狀態(tài)變量的定義 import { useClassStore } from './storea' // 獲取全局狀態(tài)變量的對象 const classStore = useClassStore() // 方式一 : 直接修改 classStore.studentNum = 36
Setup Store
// 導(dǎo)入全局狀態(tài)變量的定義 import { useStudentStore } from './storea' // 獲取全局狀態(tài)變量的對象 const studentStore = useStudentStore() // 方式一 : 直接修改 studentStore.student.className = '我也不知道是哪個班的'
方式二:$patch 方式修改
參數(shù)是一個對象的形式
Option Store
直接放入 【參數(shù)對象】 : {key:value}
就OK了
// 導(dǎo)入全局狀態(tài)變量的定義 import { useClassStore } from './storea' // 獲取全局狀態(tài)變量的對象 const classStore = useClassStore() // 方式二 : $patch 方法修改 classStore.$patch({studentNum:20})
Setup Store
由于 狀態(tài)變量在定義的時候,就是一個響應(yīng)式對象,所以需要把整個的對象都放進(jìn)去才OK。
// 導(dǎo)入全局狀態(tài)變量的定義 import { useStudentStore } from './storea' // 獲取全局狀態(tài)變量的對象 const studentStore = useStudentStore() // 方式二 : $patch 方法修改 studentStore.$patch({student:{ name: studentStore.student.name, age: studentStore.student.age, className:'又換了一個班級' }})
方式三:$patch 帶參數(shù)的方式修改
參數(shù)是 函數(shù)
的形式,且函數(shù)的參數(shù)就是 原state對象
這種方式用起來比【方式二】要更加靈活。
Option Store
// 導(dǎo)入全局狀態(tài)變量的定義 import { useClassStore } from './storea' // 獲取全局狀態(tài)變量的對象 const classStore = useClassStore() // 方式三 :$patch + 函數(shù)參數(shù)的方法修改 : 函數(shù)的參數(shù)就是 狀態(tài)對象 classStore.$patch((state)=>{ console.log(state) state.studentNum = 100 })
Setup Store
// 導(dǎo)入全局狀態(tài)變量的定義 import { useStudentStore } from './storea' // 獲取全局狀態(tài)變量的對象 const studentStore = useStudentStore() // 方式三 :$patch + 函數(shù)參數(shù)的方法修改 : 函數(shù)的參數(shù)就是 狀態(tài)對象 studentStore.$patch((state)=>{ console.log(state) state.student.className = '超級無敵快樂踢足球的班' })
方式四:actions方法的方式進(jìn)行修改
這種方式比較好理解,就是通過調(diào)用已經(jīng)定義好的方法的方式來進(jìn)行變量值的修改。
也比較推薦使用這一種方式。
Option Store
// 導(dǎo)入全局狀態(tài)變量的定義 import { useClassStore } from './storea' // 獲取全局狀態(tài)變量的對象 const classStore = useClassStore() // 方式四 :actions 方法的方式進(jìn)行數(shù)據(jù)的修改 classStore.updateName()
Setup Store
// 導(dǎo)入全局狀態(tài)變量的定義 import { useStudentStore } from './storea' // 獲取全局狀態(tài)變量的對象 const studentStore = useStudentStore() // 方式四 :actions 方法的方式進(jìn)行數(shù)據(jù)的修改 studentStore.updateName('小紅')
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中使用fetch讀取本地txt文件的技術(shù)實(shí)現(xiàn)
在Vue.js應(yīng)用開發(fā)中,有時我們需要從本地讀取文本文件(如 .txt 文件)并將其內(nèi)容展示在頁面上,這種需求在處理配置文件、日志文件或靜態(tài)數(shù)據(jù)時非常常見,本文將詳細(xì)介紹如何在Vue中使用 fetch API 讀取本地 .txt 文件,并提供多個示例和使用技巧2024-10-10Vue3?$emit用法指南(含選項API、組合API及?setup?語法糖)
這篇文章主要介紹了Vue3?$emit用法指南,使用?emit,我們可以觸發(fā)事件并將數(shù)據(jù)傳遞到組件的層次結(jié)構(gòu)中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue項目實(shí)現(xiàn)點(diǎn)擊目標(biāo)區(qū)域之外可關(guān)閉(隱藏)目標(biāo)區(qū)域
這篇文章主要介紹了vue項目實(shí)現(xiàn)點(diǎn)擊目標(biāo)區(qū)域之外可關(guān)閉(隱藏)目標(biāo)區(qū)域,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03詳解Vue.js搭建路由報錯 router.map is not a function
這篇文章主要介紹了詳解Vue.js搭建路由報錯 router.map is not a function,非常具有實(shí)用價值,需要的朋友可以參考下2017-06-06vue前端路由以及vue-router兩種模式實(shí)例詳解
路由這個概念最先是后端出現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于vue前端路由以及vue-router兩種模式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03Vue動態(tài)路由路徑重復(fù)及刷新丟失頁面問題的解決
這篇文章主要介紹了Vue動態(tài)路由路徑重復(fù)及刷新丟失頁面問題的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01