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
    评论
mapState: 这个辅助函数可以帮助我们在组件中映射 Vuex store 中的状态值。它接受一个数组或对象作为参数,用于指定我们想要映射的状态。当我们在组件中使用这个辅助函数时,它会返回一个计算属性对象,其中包含了我们所需的状态值。 例如,假设我们有一个名为`count`的状态值,我们可以使用`mapState`辅助函数将其映射到组件中: ```javascript import { mapState } from 'vuex'; export default { computed: { ...mapState(['count']) } } ``` 现在,在组件中我们就可以直接使用`count`这个计算属性来访问 Vuex store 中的`count`状态值。 mapGetters: 这个辅助函数可以帮助我们在组件中映射 Vuex store 中的 getter 方法。它跟`mapState`的用法类似,接受一个数组或对象作为参数,用于指定我们想要映射的 getter 方法。同样,它也返回一个计算属性对象。 例如,假设我们有一个名为`doubleCount`的 getter 方法,我们可以使用`mapGetters`辅助函数将其映射到组件中: ```javascript import { mapGetters } from 'vuex'; export default { computed: { ...mapGetters(['doubleCount']) } } ``` 现在,在组件中我们就可以直接使用`doubleCount`这个计算属性来访问 Vuex store 中的`doubleCount` getter 方法的返回值。 mapMutations: 这个辅助函数可以帮助我们在组件中映射 Vuex store 中的 mutation 方法。它也接受一个数组或对象作为参数,用于指定我们想要映射的 mutation 方法。不同于`mapState`和`mapGetters`,`mapMutations`返回的是一个对象,其中包含了我们所需的 mutation 方法。 例如,假设我们有一个名为`increment`的 mutation 方法,我们可以使用`mapMutations`辅助函数将其映射到组件中: ```javascript import { mapMutations } from 'vuex'; export default { methods: { ...mapMutations(['increment']) } } ``` 现在,在组件中我们就可以直接调用`increment`方法来触发 Vuex store 中的`increment` mutation。 mapActions: 这个辅助函数可以帮助我们在组件中映射 Vuex store 中的 action 方法。它同样接受一个数组或对象作为参数,用于指定我们想要映射的 action 方法。类似于`mapMutations`,`mapActions`返回的也是一个对象,其中包含了我们所需的 action 方法。 例如,假设我们有一个名为`fetchData`的 action 方法,我们可以使用`mapActions`辅助函数将其映射到组件中: ```javascript import { mapActions } from 'vuex'; export default { methods: { ...mapActions(['fetchData']) } } ``` 现在,在组件中我们就可以直接调用`fetchData`方法来触发 Vuex store 中的`fetchData` action。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值