vuex(二)

之前總結(jié)了一些vuex的基礎(chǔ)性知識(shí),只是通過一些簡單的動(dòng)作來改變 store.js 中的數(shù)據(jù)對象。這是完全無法滿足工作需求的,還需要學(xué)習(xí)一些流程判斷。
一、比如說我現(xiàn)在有這么個(gè)需求,當(dāng) count < 5 的時(shí)候,就停止 count-- 。這就需要用到 actions

**[actions](https://link.jianshu.com/?t=https%3A%2F%2Fvuex.vuejs.org%2Fzh-cn%2Factions.html)** 定義要執(zhí)行的動(dòng)作,如流程的判斷、異步請求

在 store.js 內(nèi)的 actions 中

// 定義 actions ,要執(zhí)行的動(dòng)作,如流程的判斷、異步請求
const actions ={
    increment({commit,state}){
        commit('increment') 
    },
    decrement({ commit, state }) {
        // **通過動(dòng)作改變的數(shù)據(jù),在此處來做判斷是否提交**
        if (state.count > 5) {
            commit('decrement')
        }
    }
}

運(yùn)行項(xiàng)目


4279446-ef01bbfaa66c55f6.gif
二、通過 actions 模擬異步請求
1. 先在 App.vue 中定義好事件
<template>
  <div id="app">
    <button @click="increment">增加</button>
    <button @click="decrement">減少</button>
    //異步請求事件
    <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)作,如流程的判斷、異步請求
const actions ={
    increment({commit,state}){
        commit('increment') 
    },
    decrement({ commit, state }) {
        // **通過動(dòng)作改變的數(shù)據(jù),在此處來做判斷是否提交**
        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('異步操作失敗');
        })
    }
}
1.gif
三、獲取數(shù)據(jù)狀態(tài)

假如我們需要知道數(shù)據(jù)的奇偶數(shù),那么就需要在 getters 中來判斷。

getters 中可以獲取經(jīng)過處理后的數(shù)據(jù),從而來判斷狀態(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>

4279446-828bb67e550e3114.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,049評(píng)論 0 7
  • vuex 場景重現(xiàn):一個(gè)用戶在注冊頁面注冊了手機(jī)號(hào)碼,跳轉(zhuǎn)到登錄頁面也想拿到這個(gè)手機(jī)號(hào)碼,你可以通過vue的組件化...
    sunny519111閱讀 8,164評(píng)論 4 111
  • 姓名:岳沁 學(xué)號(hào):17101223458 轉(zhuǎn)載自:http://blog.csdn.net/h5_queensty...
    丘之心閱讀 2,216評(píng)論 0 1
  • Yesterday we managed to crack the UniquePaths issue via D...
    DrunkPian0閱讀 189評(píng)論 0 0
  • 言多語失 你失態(tài)了
    幽谷DNWF閱讀 141評(píng)論 0 0

友情鏈接更多精彩內(nèi)容