美文网首页
vuex(二)

vuex(二)

作者: 写bug我是专业的 | 来源:发表于2018-05-23 20:18 被阅读0次

    之前总结了一些vuex的基础性知识,只是通过一些简单的动作来改变 store.js 中的数据对象。这是完全无法满足工作需求的,还需要学习一些流程判断。
    一、比如说我现在有这么个需求,当 count < 5 的时候,就停止 count-- 。这就需要用到 actions

    **[actions](https://link.jianshu.com/?t=https%3A%2F%2Fvuex.vuejs.org%2Fzh-cn%2Factions.html)** 定义要执行的动作,如流程的判断、异步请求
    

    在 store.js 内的 actions 中

    // 定义 actions ,要执行的动作,如流程的判断、异步请求
    const actions ={
        increment({commit,state}){
            commit('increment') 
        },
        decrement({ commit, state }) {
            // **通过动作改变的数据,在此处来做判断是否提交**
            if (state.count > 5) {
                commit('decrement')
            }
        }
    }
    

    运行项目


    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 内的 actions 中添加 异步 Promise 事件

    // 定义 actions ,要执行的动作,如流程的判断、异步请求
    const actions ={
        increment({commit,state}){
            commit('increment') 
        },
        decrement({ commit, state }) {
            // **通过动作改变的数据,在此处来做判断是否提交**
            if (state.count > 5) {
                commit('decrement')
            }
        },
        incrementAsync({commit,state}){
            // 模拟异步操作
            var a = new Promise((resolve,reject) => {
                setTimeout(() => {
                    resolve();
                }, 3000);
            })
            // 3 秒之后提交一次 increment ,也就是执行一次 increment 
            a.then(() => {
                commit('increment')
            }).catch(() => {
                console.log('异步操作失败');
            })
        }
    }
    
    1.gif
    三、获取数据状态

    假如我们需要知道数据的奇偶数,那么就需要在 getters 中来判断。

    getters 中可以获取经过处理后的数据,从而来判断状态
    

    在 store.js 的 getters 中加入判断奇偶数的方法

    var getters={
        count(state){
            return state.count
        },
        EvenOrOdd(state){
            return state.count%2==0 ? '偶数' : '奇数'
        }
    }
    

    在 App.vue 中加入

    <template>
      <div id="app">
        <button @click="increment">增加</button>
        <button @click="decrement">减少</button>
        <button @click="incrementAsync">异步增加</button>
        <h1>{{count}}</h1>
        <!-- 判断奇偶数的方法  这种写法它会自动调用 EvenOrOdd 方法中的返回值,拿到的是个属性 -->
        <h1>{{EvenOrOdd}}</h1>
      </div>
    </template>
    
    <script>
    import {mapGetters,mapActions} from 'vuex'
    export default {
      name: 'app',
      computed:mapGetters([
        // 判断奇偶数的方法
        'EvenOrOdd',
        'count'
      ]),
      methods:mapActions([
        'increment',
        'decrement',
        'incrementAsync'
      ])
    }
    </script>
    
    
    4279446-828bb67e550e3114.gif

    相关文章

      网友评论

          本文标题:vuex(二)

          本文链接:https://www.haomeiwen.com/subject/wrjdjftx.html