Vuex学习

Vuex学习(计算器)

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        add(state){
            state.count++
        },
        subtract(state){
            state.count--
        },
        addN(state,step){
            state.count += step
        },
        subtractN(state,step){
            state.count-=step
        }
    },
    actions: {
        addAsync(context){
            setTimeout(() => {
                context.commit('add')
            }, 1000);
        },
        addAsyncS(context,step){
            setTimeout(() => {
                context.commit('addN',step)
            }, 1000);
        },
        addAsyncJ(context,step){
            setTimeout(() => {
                context.commit('subtractN',step)
            }, 1000);
        },
    },
计算器加号组件
<template>
  <div>
      <h2>当前最新的值为:{{ $store.state.count }}</h2>
      <button @click="btnHandler1">+1</button>
      <button @click="btnHandler3">+3</button>
      <button @click="btnHandler4">延迟一秒+1</button>
      <button @click="btnHandler5">延迟一秒+3</button>
      <h4>{{ $store.getters.showNum  }}</h4>
  </div>
</template>

<script>
export default {
    data(){
        return{};
    },
    methods:{
        btnHandler1(){
            this.$store.commit('add')
            // this.$store.state.count++
        },
        btnHandler3(){
            this.$store.commit('addN',3)
        },
        // 异步让count加3
        btnHandler4(){
            this.$store.dispatch('addAsync')
        },
        btnHandler5(){
            this.$store.dispatch('addAsyncS',3)
        }
    }
}
</script>

<style>

</style>
计算器减法组件
<template>
  <div>
    <h2>当前最新的值为:{{ count }}</h2>
    <button @click="btnHJandler1">-1</button>
    <button @click="btnHJandler2">-3</button>
    <button @click="btnHJandler3">延迟一秒-3</button>
    <h3>当前最新的值为:{{ showNum }}</h3>

  </div>
</template>

<script>
import { mapState, mapMutations,mapActions,mapGetters } from "vuex";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["showNum"]),
  },
  methods: {
      ...mapMutations(["subtract", "subtractN"]),
      ...mapActions(['addAsyncJ']),
    btnHJandler1() {
      this.subtract()
    },
    btnHJandler2() {
      this.subtractN(3)
    },
    btnHJandler3(){
        this.addAsyncJ(3)
    }
  },
};
</script>

<style>
</style>
知识点总结
  • state

    • Vuex中代码
    	state: {
     	   count: 0
    	 },
    
    • 此处是Vuex存储的地方
    • 在组件中使用的两种方法
    • <h2>当前最新的值为:{{ $store.state.count }}</h2>
      
    •  <h2>当前最新的值为:{{ count }}</h2>
      
      import { mapState } from "vuex";
      
      computed: {
      ...mapState(["count"]),
       },
      
  • mutations

    • Vuex中代码
    mutations: {
    	addN(state,step){
            	state.count += step
        	},
        }
    
    • 修改数据的地方
    • 在组件中使用的两种方法
      	btnHandler3(){
           this.$store.commit('addN',3)
      },
      
      import {  mapMutations } from "vuex";
      methods:{
      ...mapMutations(["addN"]),
       btnHJandler2() {
      		 this.addN(3)
      	 },
      }
      
  • actions

    • Vuex中代码
     actions: {
     	addAsyncS(context,step){
          	  // 异步行为 
          	  // 这里的context就类似于newVuex的实力对象
          	  setTimeout(() => {
                context.commit('addN',step)
          	  }, 1000);
       	 },
      }
    
    • 这里是进行异步操作的
    • 这里不可以直接操作state中的值
    • 只能通过异步mutation中的方法
    • 在组件中使用的两种方法
       btnHandler5(){
          this.$store.dispatch('addAsyncS',3)
      }
      
      import { mapActions } from "vuex";
      methods: {
      	  ...mapActions(['addAsyncS']),
      	  btnHJandler3(){
      		this.addAsyncS(3)
      	 }
      }
      
  • getters

    • Vuex中代码
    	getters:{
        	showNum:state=>{
      	      return '当前最新的数量是['+ state.count +']'
      	  }
      },
    
    • getters不会去修改原数据他出去完成之后会形成一个新的数据类似于计算属性
    • statez中的数据发生改变getters里面也会改变
    • 在组件中使用的两种方法
      <h4>{{ $store.getters.showNum  }}</h4>
      
      <h3>当前最新的值为:{{ showNum }}</h3>
      computed: {
      	...mapGetters(["showNum"]),
      },
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值