Vuex学习笔记

在这里插入图片描述

store.js

// vuex的核心管理模块

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

//状态对象
const state={//初始化状态
    count: 0
}
//包含多个更新state函数的对象
const mutations={
    //增加的mutation
    INCREMENT() {
        state.count++
    },
    //减少的mutation
    DECREMENT() {
        state.count--
    }
}
//包含多个对应事件回调函数的对象
const actions={
//增加的action
    increment({commit}) {
        //提交增加的mutation
        commit('INCREMENT')
    },
    //减少的action
    decrement({commit}) {
        commit('DECREMENT')
    },
    //带条件的action
    incrementIfOdd({commit,state}){
        if(state.count%2===1){
            commit('INCREMENT')
        }
    },
    //异步的action
    incrementAsync({commit}){
        setTimeout(()=>{
            commit('INCREMENT')
        },1000)
    },
}
//包含多个getter计算属性函数的对象
const getters={
    evenOrOdd(state) {
        return state.count % 2 === 0 ? '偶数' : '奇数';
    }
}

export default new Vuex.Store({
    state,//状态对象
    mutations,//包含多个更新state函数的对象
    actions,//包含多个对应事件回调函数的对象
    getters,//包含多个getter计算属性函数的对象
})

VueTest.vue

<template>
    <div>
        <!--    <p>click {{$store.state.count}} times, count is {{evenOrOdd}}</p>-->
        <p>click {{count}} times, count is {{evenOrOdd}}</p>
        <el-button type="primary" @click="increment">+</el-button>
        <el-button type="primary" @click="decrement">-</el-button>
        <el-button type="primary" @click="incrementIfOdd">如果是奇数增加</el-button>
        <el-button type="primary" @click="incrementAsync">延迟增加</el-button>
    </div>
</template>

<script>
    import { mapActions, mapState, mapGetters } from 'vuex';

    export default {
        name: 'VuexTest',
        computed: {
            ...mapState(['count']),//mapState()返回值:{count(){return this.$store.state['count']}}
            ...mapGetters(['evenOrOdd'])//mapGetters()返回值:{evenOrOdd(){return this.$store.getters['evenOrOdd']}}
            // evenOrOdd() {
            //     //return this.count % 2 === 0 ? '偶数' : '奇数';
            //     // return this.$store.getters.evenOrOdd
            // }
        },
        methods:{
            ...mapActions(['increment','decrement','incrementIfOdd','incrementAsync'])
        }
        // methods: {
        //     //增加
        //     increment() {
        //         // const count = this.count;
        //         // this.count = count + 1;
        //         this.$store.dispatch('increment');//触发store中对应action
        //     },
        //     //减少
        //     decrement() {
        //         // const count = this.count;
        //         // this.count = count - 1;
        //         this.$store.dispatch('decrement');//触发store中对应action
        //     },
        //     //奇数增加
        //     incrementIfOdd() {
        //         this.$store.dispatch('incrementIfOdd');//触发store中对应action
        //         // const count = this.count;
        //         // if(count%2===1){
        //         //     this.count=count+1
        //         // }
        //     },
        //     //过一秒增加
        //     incrementAsync() {
        //         this.$store.dispatch('incrementAsync');//触发store中对应action
        //         // setTimeout(()=>{
        //         //     const count = this.count;
        //         //     this.count = count + 1;
        //         // },1000)
        //     }
        // }
    };
</script>

<style scoped>

</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值