vuex辅助函数mapState,mapGetters,mapMutations,mapActions的应用

项目目录下如:

Vuex辅助函数的运用——mapState应用4步骤:


Vuex辅助函数的运用——mapState应用第1步:在store/index.js里面设置username。

index.js代码如下:

// store/index.js内容

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    username:'longfei' //Vuex系列的辅助函数的运用——mapState应用第1步:在store/index.js里面设置username。
  },
  getters: {
  },
  mutations: {  
    increment(state){
      state.count++
    },
    decrement(state){
      state.count--
    },
    // incrementAsync(state){ 
    //   setTimeout(()=>{
    //     state.count++;
    //   },1000);
    // }
    incrementAsync(state){  
      state.count++;
    }
  },
  actions: {  
    increment({commit}){
      commit('increment')
    },
    decrement({commit}){
      commit('decrement')
    },
    incrementAsync({commit}){  
      setTimeout(()=>{
        commit('incrementAsync');
      },1000);
    }
  },
  modules: {
  }
})


//Vuex系列的辅助函数的运用——mapState应用4步骤:
//Vuex系列的辅助函数的运用——mapState应用第1步:在store/index.js里面设置username。
//Vuex系列的辅助函数的运用——mapState应用第2步:在About.vue里面导入方法。
//Vuex系列的辅助函数的运用——mapState应用第3步:在About.vue里面获取count和username。
//Vuex系列的辅助函数的运用——mapState应用第4步:在About.vue里面展示count和username。


Vuex辅助函数的运用——mapState应用第2步:在About.vue里面导入方法。
Vuex辅助函数的运用——mapState应用第3步:在About.vue里面获取count和username。
Vuex辅助函数的运用——mapState应用第4步:在About.vue里面展示count和username。

About.vue代码:

// About.vue内容

<template>
  <div class="about">
    <h1>This is an about page</h1>
    {{myCount}}--{{user}}   <!-- //Vuex系列的辅助函数的运用——mapState应用第4步:在About.vue里面展示count和username。 -->
    {{count}} 
    <button @click="increment">+1</button>  
    <button @click="decrement">-1</button>
    <button @click="incrementAsync">+1异步</button>  
  </div>
</template>
<script>
import { mapState,mapGetters,mapMutations,mapActions } from 'vuex'; //Vuex系列的辅助函数的运用——mapState应用第2步:在About.vue里面导入方法。
  export default{
    computed:{  
      // count(){
      //   return this.$store.state.count;
      // }
      ...mapState({ //Vuex系列的辅助函数的运用——mapState应用第3步:在About.vue里面获取count和username。
        myCount:'count',
        user:'username'
      })
    },
    methods:{ 
      increment(){
        this.$store.commit('increment');
      },
      decrement(){
        this.$store.commit('decrement');
      },
      incrementAsync(){  
        this.$store.commit('incrementAsync');
      }
    }
  }
</script>

运行结果如下:


Vuex系列的辅助函数的运用——getter应用3步骤:

Vuex系列的辅助函数的运用——getter应用第1步:在store/index.js里面设置方法。

// store/index.js内容

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    username:'longfei' //Vuex系列的辅助函数的运用——mapState应用第1步:在store/index.js里面设置username。
  },
  getters: {
    evenOrOdd(state){   //Vuex系列的辅助函数的运用——getter应用第1步:在store/index.js里面设置方法。
      return state.count % 2 === 0 ? '偶数' : '奇数'
    }
  },
  mutations: {  
    increment(state){
      state.count++
    },
    decrement(state){
      state.count--
    },
    // incrementAsync(state){ 
    //   setTimeout(()=>{
    //     state.count++;
    //   },1000);
    // }
    incrementAsync(state){  
      state.count++;
    }
  },
  actions: {  
    increment({commit}){
      commit('increment')
    },
    decrement({commit}){
      commit('decrement')
    },
    incrementAsync({commit}){  
      setTimeout(()=>{
        commit('incrementAsync');
      },1000);
    }
  },
  modules: {
  }
})


//Vuex系列的辅助函数的运用——mapState应用4步骤:
//Vuex系列的辅助函数的运用——mapState应用第1步:在store/index.js里面设置username。
//Vuex系列的辅助函数的运用——mapState应用第2步:在About.vue里面导入方法。
//Vuex系列的辅助函数的运用——mapState应用第3步:在About.vue里面获取count和username。
//Vuex系列的辅助函数的运用——mapState应用第4步:在About.vue里面展示count和username。


//Vuex系列的辅助函数的运用——getter应用3步骤:
//Vuex系列的辅助函数的运用——getter应用第1步:在store/index.js里面设置方法。
//Vuex系列的辅助函数的运用——getter应用第2步:在About.vue里面设置mapGetters函数。
//Vuex系列的辅助函数的运用——getter应用第3步:在About.vue里面展示evenOrOdd。

Vuex系列的辅助函数的运用——getter应用第2步:在About.vue里面设置mapGetters函数。
Vuex系列的辅助函数的运用——getter应用第3步:在About.vue里面展示evenOrOdd。

// About.vue内容

<template>
  <div class="about">
    <h1>This is an about page</h1>
    {{myCount}}--{{user}}   <!-- //Vuex系列的辅助函数的运用——mapState应用第4步:在About.vue里面展示count和username。 -->
    {{count}} 
    {{evenOrOdd}}
    <button @click="increment">+1</button>  
    <button @click="decrement">-1</button>
    <button @click="incrementAsync">+1异步</button>  
  </div>
</template>
<script>
import { mapState,mapGetters,mapMutations,mapActions } from 'vuex'; //Vuex系列的辅助函数的运用——mapState应用第2步:在About.vue里面导入方法。
  export default{
    computed:{  
      // count(){
      //   return this.$store.state.count;
      // }
      ...mapState({ //Vuex系列的辅助函数的运用——mapState应用第3步:在About.vue里面获取count和username。
        myCount:'count',
        user:'username'
      }),
      ...mapGetters(['evenOrOdd'])  //Vuex系列的辅助函数的运用——getter应用第2步:在About.vue里面设置mapGetters函数。
    },
    methods:{ 
      increment(){
        this.$store.commit('increment');
      },
      decrement(){
        this.$store.commit('decrement');
      },
      incrementAsync(){  
        this.$store.commit('incrementAsync');
      }
    }
  }
</script>

运行结果:


Vuex系列的辅助函数的运用——mapMutations和mapActions方法各一步。 

在About.vue里面设置,效果一样:

About.vue代码:

// About.vue内容

<template>
  <div class="about">
    <h1>This is an about page</h1>
    {{myCount}}--{{user}}   <!-- //Vuex系列的辅助函数的运用——mapState应用第4步:在About.vue里面展示count和username。 -->
    {{count}} 
    {{evenOrOdd}}
    <button @click="increment">+1</button>  
    <button @click="decrement">-1</button>
    <button @click="incrementAsync">+1异步</button>  
  </div>
</template>
<script>
import { mapState,mapGetters,mapMutations,mapActions } from 'vuex'; //Vuex系列的辅助函数的运用——mapState应用第2步:在About.vue里面导入方法。
  export default{
    computed:{  
      // count(){
      //   return this.$store.state.count;
      // }
      ...mapState({ //Vuex系列的辅助函数的运用——mapState应用第3步:在About.vue里面获取count和username。
        myCount:'count',
        user:'username'
      }),
      ...mapGetters(['evenOrOdd'])  //Vuex系列的辅助函数的运用——getter应用第2步:在About.vue里面设置mapGetters函数。
    },
    methods:{ 
      // increment(){
      //   this.$store.dispatch('increment');
      // },
      // decrement(){
      //   this.$store.dispatch('decrement');
      // },
      ...mapMutations(['increment','decrement']),   //Vuex系列的辅助函数的运用——mapMutations 方法

      // incrementAsync(){  
      //   this.$store.dispatch('incrementAsync');
      // }
      ...mapActions(['incrementAsync']),    //Vuex系列的辅助函数的运用——mapActions 方法
    }
  }
</script>

运行结果如下:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值