Vue3中使用Pinia修改State的五種方式
修改Pinia倉庫的值有5種方式
src/store/index.ts
import { defineStore } from 'pinia';
import { Names } from './store-name';
export const useTestStore = defineStore(Names.Test, {
state:()=>{
return {
current:1111,
name: '小滿111'
}
},
getters:{ // 類似computed計算屬性 同樣有緩存的
},
actions:{ // 類似 methods方法 可以做同步、異步操作 提交state
}
});第一種修改State的方式
<template>
<div>
pinia: {{ Test.current }} --- {{ Test.name }}
<button @click="change">change</button>
</div>
</template>
<script setup lang="ts">
import {useTestStore} from './store';
const Test = useTestStore();
// 第一種修改State的方式
const change = () => {
Test.current++
}
</script>
<style scoped>
</style>第二種修改State的方式
<template>
<div>
pinia: {{ Test.current }} --- {{ Test.name }}
<button @click="change">change</button>
</div>
</template>
<script setup lang="ts">
import {useTestStore} from './store';
const Test = useTestStore();
// 第二種修改State的方式 批量修改對象屬性
const change = () => {
Test.$patch({
current: 999,
name: '李四'
})
}
</script>
<style scoped>
</style>第三種修改State的方式
<template>
<div>
pinia: {{ Test.current }} --- {{ Test.name }}
<button @click="change">change</button>
</div>
</template>
<script setup lang="ts">
import {useTestStore} from './store';
const Test = useTestStore();
// 第三種修改State的方式 向$patch中傳入工廠函數(shù) 可以寫邏輯
const change = () => {
Test.$patch((state) => {
if(state.current>1113){
state.current--;
state.name = '羅翔老師';
} else {
state.current++;
state.name = '羅永浩老師';
}
})
}
</script>
<style scoped>
</style>第四種修改State的方式
<template>
<div>
pinia: {{ Test.current }} --- {{ Test.name }}
<button @click="change">change</button>
</div>
</template>
<script setup lang="ts">
import {useTestStore} from './store';
const Test = useTestStore();
// 第四種修改State的方式 覆蓋這個state對象
const change = () => {
Test.$state = {
current: 1024,
name: '羅城將軍'
}
}
</script>
<style scoped>
</style>第五種修改State的方式
調(diào)用actions里面的方式
在 src/store/index.ts 里面的actions里面寫個方法
import { defineStore } from 'pinia';
import { Names } from './store-name';
export const useTestStore = defineStore(Names.Test, {
state:()=>{
return {
current:1111,
name: '小滿111'
}
},
getters:{ // 類似computed計算屬性 同樣有緩存的
},
actions:{ // 類似 methods方法 可以做同步、異步操作 提交state
setCurrent(num:number){ // 注意此處不要寫箭頭函數(shù),否則this指向就不對了
this.current = num;
}
}
});再在組件里面調(diào)用
<template>
<div>
pinia: {{ Test.current }} --- {{ Test.name }}
<button @click="change">change</button>
</div>
</template>
<script setup lang="ts">
import {useTestStore} from './store';
const Test = useTestStore();
// 第五種
const change = () => {
Test.setCurrent(5173); // 直接調(diào)用setCurrent函數(shù)
}
</script>
<style scoped>
</style>到此這篇關(guān)于Vue3中使用Pinia修改State的方法的文章就介紹到這了,更多相關(guān)vue3使用Pinia修改state內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue如何將后臺返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字
這篇文章主要介紹了vue如何將后臺返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案
這篇文章主要為大家介紹了微信小程序Echarts動態(tài)使用及圖表層級踩坑解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
vue登錄以及權(quán)限驗證相關(guān)的實現(xiàn)
這篇文章主要介紹了vue登錄以及權(quán)限驗證相關(guān)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vue3+element Plus如何實現(xiàn)彈框的拖拽、可點擊底層頁面功能
這篇文章主要介紹了vue3+element Plus如何實現(xiàn)彈框的拖拽、可點擊底層頁面功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11
VUE多個下拉框?qū)崿F(xiàn)雙向聯(lián)動效果
這篇文章主要為大家詳細(xì)介紹了VUE多個下拉框?qū)崿F(xiàn)雙向聯(lián)動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07

