vuex-mapState,mapGetters,mapMutations,mapActions 的使用方法

组件中写法:

state获取的方法

$store.state.user.count

...mapState(['userIndex'])

...mapState({userIndex: state => state.user.user}),

state定义

  state: {
    name: '用户',
    todos: [
      { id: 1, done: true, text: '我是码农' },
      { id: 2, done: false, text: '我是码农202号' },
      { id: 3, done: true, text: '我是码农202号' }
    ],
    count: 0,
    count2: 0
  },

 


getters

$store.getters['user/gettersName1'] // $store.getters查看打印结果
 
...mapGetters('user', {gettersName2: 'gettersName1'   }),

...mapGetters(['gettersName1']),

...mapGetters('user', ['gettersName']),
  getters: {
    gettersName1 () {
      return 'user-getters-name1'
    },
    gettersName2 () {
      return 'user-getters-name1'
    }
  },

 


mutations

 this.$store.commit('user/add', 5)

 ...mapMutations('user', ['sum']) //参数放在sum(5).

对应的js文件中的mutations

  mutations: {
    add: (state, data) => {
      state.count += data
    },
    sum: (state, data) => {
      state.count2 += data
    }
  },

 


Actions

addYibu1 () {
   this.$store.dispatch('user/addAction1', 5)
},

...mapActions('user', ['addAction2']),

对应js里面的action

  actions: {
    addAction: (context, data) => {
      context.commit('sum', data)
    },
    //方式2
    addAction1 ({ commit }, data) {
      commit('sum', data)
    },
    addAction2 ({ commit }, data) {
      commit('sum', data)
    },
    addAction3 ({ commit }, data) {
      commit('sum', data)
    }

  }

 

 

 

<template>
  <div class="hello">
    <h1>mapState</h1>
    <h1>index.js【mapState】:{{IndexName}}</h1>
    <h1>user.js【mapState】方式1:{{userName1}}</h1>
    <h1>user.js【mapState】方式2:{{userName2}}</h1>
    <h1>user store方式2:{{$store.state.user.name}}</h1>
    <hr>
    <h1>getter</h1>
    <h2>index.js--gettersName1:{{gettersName1}}</h2>
    <h2>index.js--gettersName12:{{gettersName2}}</h2>
    <h2>index.js--gettersName13:{{gettersName}}</h2>
    <h2>index.js--gettersName13:{{newName}}</h2>
    <h3>store.getters:{{$store.getters}}</h3>
    <h3>store.getters:{{$store.getters['user/gettersName1']}}</h3>
    <hr>
    <div><button @click="add">触发user.js-add</button> {{$store.state.user.count}}</div>
    <div><button @click="sum(5)">触发user.js-add</button> {{$store.state.user.count2}}</div>
    <!-- <div><button @click="add2(5)">触发user.js-add</button> {{$store.state.user.count}}</div> -->
    <hr>
    <div> <button @click="addYibu">异步按钮</button> </div>
    <div> <button @click="addYibu1">$store.dispatch异步按钮1</button> </div>
    <div> <button @click="addAction2(5)">addAction2异步按钮2</button> </div>
    <div> <button @click="addAction3(10)">addAction3异步按钮3</button> </div>
    <!-- <div> <button @click="addYibu3">异步按钮3</button> </div> -->

  </div>
</template>

<script>

import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'


export default {
  name: 'HelloWorld',
  data () {
    return {
      newName: ''
    }
  },
  computed: {

    ...mapGetters(['gettersName1']),

    // 替换变量名
    ...mapGetters('user', {
      gettersName2: 'gettersName1'
    }),
    ...mapGetters('user', ['gettersName']),

    // 方式一
    // ...mapState(['userIndex'])

    // ...mapState({
    //   userIndex: state => state.user.user
    // }),

    ...mapState({
      IndexName: state => state.name
    }),

    ...mapState('user', {
      userName1: state => state.name
    }),

    ...mapState({
      userName2: state => state.user.name
    }),



  },
  mounted () {
    this.newName = this.gettersName + '加载后'
  },
  methods: {
    add () {
      this.$store.commit('user/add', 5)
    },
    // ...mapMutations('user', ['add']),
    ...mapMutations('user', ['sum']),

    // 异步提交
    addYibu () {
      this.$store.dispatch('user/addAction', 5)
    },
    addYibu1 () {
      this.$store.dispatch('user/addAction1', 5)
    },

    ...mapActions('user', ['addAction2']),
    ...mapActions('user', ['addAction3']),


  }

}

</script>

 

store->index.js

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

import user from './module/user'
import shop from './module/shop'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    name: 'index Name',
  },
  getters: {
    gettersName1: () => {
      return '【index-gettersName1】'
    },
    gettersName4: () => {
      return '【index-gettersName1-new】'
    }
  },
  mutations: {
    set_data (state, data) {
      state.user = data
    }
  },
  actions: {
    setData ({ commit }, data) {
      commit('set_data', data)
    }
  },
  modules: {
    user,
    shop
  }
})

store---module---user.js

export default {
  namespaced: true,
  state: {
    name: '用户',
    todos: [
      { id: 1, done: true, text: '我是码农' },
      { id: 2, done: false, text: '我是码农202号' },
      { id: 3, done: true, text: '我是码农202号' }
    ],
    count: 0,
    count2: 0
  },
  getters: {
    gettersName1 () {
      return 'user-getters-name1'
    },
    gettersName2 () {
      return 'user-getters-name1'
    }
  },
  mutations: {
    add: (state, data) => {
      state.count += data
    },
    sum: (state, data) => {
      state.count2 += data
    }
  },
  actions: {
    addAction: (context, data) => {
      context.commit('sum', data)
    },
    //方式2
    addAction1 ({ commit }, data) {
      commit('sum', data)
    },
    addAction2 ({ commit }, data) {
      commit('sum', data)
    },
    addAction3 ({ commit }, data) {
      commit('sum', data)
    }

  }
}

git :https://gitee.com/xnmde8/vuex_learning_organization.git

 

  • 0
    点赞
  • 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` mutationmapActions: 这个辅助函数可以帮助我们在组件中映射 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、付费专栏及课程。

余额充值