【Vue基础十三】---Vuex的求和案例--map辅助函数的使用

一、 普通求和案例【不使用vuex】

1-1 展示

在这里插入图片描述

1-2 代码

1-2-1 目录结构

在这里插入图片描述

1-2-2 App.vue
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <Count></Count>
  </div>
</template>

<script>
import Count from "./components/Count.vue";

export default {
  name: "App",
  components: {
    Count,
  },
};
</script>
1-2-3 Count.vue
<template>
  <div>
    <h1>当前求和为:{{ sum }}</h1>
    <select name="" id="" v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment(n)">+</button>
    <button @click="decrement(n)">-</button>
    <button @click="incrementOdd">当前求和为奇数再加</button>
    <button @click="incrementWait">等一等再+</button>
  </div>
</template>

<script>
export default {
  name: "Count",
  data() {
    return {
      n: 1,
      sum: 0,
    };
  },
  methods: {
    increment() {
      this.sum += this.n;
    },
    decrement() {
      this.sum -= this.n;
    },
    incrementOdd() {
      if (this.sum % 2 !== 0) {
        this.sum += this.n;
      }
    },
    incrementWait() {
      setTimeout(() => {
        this.sum += this.n;
      });
    },
  },
};
</script>

<style>
</style>

二、 Vuex改写求和案例

2-1 目录结构

在这里插入图片描述

2-2 结果展示

在这里插入图片描述

2-3 代码

2-3-1 App.vue
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <Count></Count>
  </div>
</template>

<script>
import Count from "./components/Count.vue";

export default {
  name: "App",
  components: {
    Count,
  },
  mounted() {
    console.log("App", this);
  },
};
</script>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

2-3-2 main.js
import Vue from 'vue'
import App from './App.vue'


// 引入store
import store from './store/index'

Vue.config.productionTip = false

new Vue({
    render: h => h(App),
    store
}).$mount('#app')
2-3-3 conponent下的Count.vue
<template>
  <div>
    <h1>当前求和为:{{ sum }}</h1>
    <select name="" id="" v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment(n)">+</button>
    <button @click="decrement(n)">-</button>
    <button @click="incrementOdd(n)">当前求和为奇数再加</button>
    <button @click="incrementWait(n)">等一等再+</button>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Count",
  data() {
    return {
      n: 1,
      // sum: 0,
    };
  },
  computed: {
    // sum() {
    //   return this.$store.state.sum;
    // },
    ...mapState(["sum"]),
  },
  methods: {
    ...mapMutations({ increment: "JIA", decrement: "JIAN" }),
    ...mapActions({ incrementOdd: "jiaOdd", incrementWait: "jiaWait" }),
    // increment() {
    //   this.sum += this.n;
    // },
    // decrement() {
    //   this.sum -= this.n;
    // },
    // incrementOdd() {
    //   // if (this.sum % 2 !== 0) {
    //   //   this.sum += this.n;
    //   // }
    //   this.$store.dispatch("jiaOdd", this.n);
    // },
    // incrementWait() {
    //   // setTimeout(() => {
    //   //   this.sum += this.n;
    //   // });
    //   this.$store.dispatch("jiaWait", this.n);
    // },
  },
};
</script>

<style>
</style>
2-3-4 store下的index.js
// 该文件用于创建Vuex中最核心的store
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// actions---相应组件中的动作
const actions = {
        jiaOdd(context, value) {
            if (context.state.sum % 2 !== 0) {
                context.commit('JIA', value)
            }
        },
        jiaWait(context, value) {
            setTimeout(() => {
                context.commit('JIA', value)
            }, 500)
        }
    }
    // mutations -- 操作数据(state)
const mutations = {
        JIA(state, value) {
            state.sum += value;
        },
        JIAN(state, value) {
            state.sum -= value;
        },
    }
    // state -- 存储数据
const state = {
    sum: 0,
}
const getters = {}

const store = new Vuex.Store({
    actions: actions,
    mutations,
    state,
    getters
})
export default store
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值