Vuex提升學(xué)習(xí)篇
本文介紹了Vuex 提升學(xué)習(xí)教程。如果本篇有看不明白的地方,請(qǐng)移步上一篇:Vuex 入門
上一篇我們講了如何通過一些簡(jiǎn)單的動(dòng)作來(lái)改變 store.js 中的數(shù)據(jù)對(duì)象,在實(shí)際工作中,這是完全無(wú)法滿足工作需求的,所以這篇我們來(lái)說(shuō)說(shuō)如何做一些簡(jiǎn)單的流程判斷。

自制vuex LOGO
一、比如說(shuō)我現(xiàn)在有這么個(gè)需求,當(dāng) count < 5 的時(shí)候,就停止 count-- 。這就需要用到 actions
actions 定義要執(zhí)行的動(dòng)作,如流程的判斷、異步請(qǐng)求
在 store.js 內(nèi)的 actions 中
// 定義 actions ,要執(zhí)行的動(dòng)作,如流程的判斷、異步請(qǐng)求
const actions ={
increment({commit,state}){
commit('increment')
},
decrement({ commit, state }) {
// **通過動(dòng)作改變的數(shù)據(jù),在此處來(lái)做判斷是否提交**
if (state.count > 5) {
commit('decrement')
}
}
}
運(yùn)行項(xiàng)目

二、通過 actions 模擬異步請(qǐng)求
1. 先在 App.vue 中定義好事件
<template>
<div id="app">
<button @click="increment">增加</button>
<button @click="decrement">減少</button>
//異步請(qǐng)求事件
<button @click="incrementAsync">異步增加</button>
<h1>{{count}}</h1>
</div>
</template>
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
name: 'app',
computed:mapGetters([
'count'
]),
methods:mapActions([
'increment',
'decrement',
'incrementAsync'
])
}
</script>
2. 在 store.js 內(nèi)的 actions 中添加 異步 Promise 事件
// 定義 actions ,要執(zhí)行的動(dòng)作,如流程的判斷、異步請(qǐng)求
const actions ={
increment({commit,state}){
commit('increment')
},
decrement({ commit, state }) {
// **通過動(dòng)作改變的數(shù)據(jù),在此處來(lái)做判斷是否提交**
if (state.count > 5) {
commit('decrement')
}
},
incrementAsync({commit,state}){
// 模擬異步操作
var a = new Promise((resolve,reject) => {
setTimeout(() => {
resolve();
}, 3000);
})
// 3 秒之后提交一次 increment ,也就是執(zhí)行一次 increment
a.then(() => {
commit('increment')
}).catch(() => {
console.log('異步操作失敗');
})
}
}
運(yùn)行項(xiàng)目

三、獲取數(shù)據(jù)狀態(tài)
假如我們需要知道數(shù)據(jù)的奇偶數(shù),那么就需要在 getters 中來(lái)判斷。
getters 中可以獲取經(jīng)過處理后的數(shù)據(jù),從而來(lái)判斷狀態(tài)
在 store.js 的 getters 中加入判斷奇偶數(shù)的方法
var getters={
count(state){
return state.count
},
EvenOrOdd(state){
return state.count%2==0 ? '偶數(shù)' : '奇數(shù)'
}
}
在 App.vue 中加入
<template>
<div id="app">
<button @click="increment">增加</button>
<button @click="decrement">減少</button>
<button @click="incrementAsync">異步增加</button>
<h1>{{count}}</h1>
<!-- 判斷奇偶數(shù)的方法 這種寫法它會(huì)自動(dòng)調(diào)用 EvenOrOdd 方法中的返回值,拿到的是個(gè)屬性 -->
<h1>{{EvenOrOdd}}</h1>
</div>
</template>
<script>
import {mapGetters,mapActions} from 'vuex'
export default {
name: 'app',
computed:mapGetters([
// 判斷奇偶數(shù)的方法
'EvenOrOdd',
'count'
]),
methods:mapActions([
'increment',
'decrement',
'incrementAsync'
])
}
</script>

判斷奇偶數(shù).gif
如有不明白之處,還請(qǐng)留言交流,或者直接翻看 API
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue.js實(shí)戰(zhàn)之使用Vuex + axios發(fā)送請(qǐng)求詳解
- Vuex之理解Store的用法
- 如何使用Vuex+Vue.js構(gòu)建單頁(yè)應(yīng)用
- 詳解vuex 中的 state 在組件中如何監(jiān)聽
- Vuex之理解Mutations的用法實(shí)例
- Vue.js實(shí)戰(zhàn)之Vuex的入門教程
- Vuex之理解Getters的用法實(shí)例
- 基于vue2.0+vuex的日期選擇組件功能實(shí)現(xiàn)
- Vuex之理解state的用法實(shí)例
- vue+vuex+axio從后臺(tái)獲取數(shù)據(jù)存入vuex實(shí)現(xiàn)組件之間共享數(shù)據(jù)
相關(guān)文章
Element-UI中<el-cascader?/>回顯失敗問題的完美解決
我們?cè)谑褂胑l-cascader控件往數(shù)據(jù)庫(kù)保存的都是最后一級(jí)的數(shù)據(jù),那如果再次編輯此條數(shù)據(jù)時(shí),直接給el-cascader傳入最后一級(jí)的數(shù)據(jù),它是不會(huì)自動(dòng)勾選的,下面這篇文章主要給大家介紹了關(guān)于Element-UI中<el-cascader?/>回顯失敗問題的完美解決辦法,需要的朋友可以參考下2023-01-01
vue Element UI 解決表格數(shù)據(jù)不更新問題及解決方案
在使用Vue和ElementUI開發(fā)后臺(tái)管理系統(tǒng)時(shí),可能會(huì)遇到表格數(shù)據(jù)不更新的問題,這通常是因?yàn)閂ue的響應(yīng)式系統(tǒng)未檢測(cè)到數(shù)據(jù)變化或數(shù)據(jù)更新后未正確觸發(fā)視圖的重新渲染,本文給大家介紹vue Element UI 解決表格數(shù)據(jù)不更新問題,感興趣的朋友一起看看吧2024-10-10
vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式
這篇文章主要介紹了vue3系統(tǒng)進(jìn)入頁(yè)面前的權(quán)限判斷和重定向方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式
最近的項(xiàng)目完成后,在性能優(yōu)化階段需要做按鈕的防止重復(fù)點(diǎn)擊功能,本文主要介紹了vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
前端Vue設(shè)置cookie、刪除cookie,獲取cookie方式
這篇文章主要介紹了前端Vue設(shè)置cookie、刪除cookie,獲取cookie方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue three.js創(chuàng)建地面并設(shè)置陰影實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue three.js創(chuàng)建地面并設(shè)置陰影實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

