vuex中各属性的使用

vuex

状态管理模式
state:共享数据源;
mutations:修改数据(state中的数据);
actions:异步请求;
getters:对共享数据进行包装(类似计算属性);
modules: 模块化

代码

vuex代码

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

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0,
    arr: [1, 3, 6, 7, 4, 10]
  },
  mutations: {
    addCount(state, payload) {
      state.count += payload
    }
  },
  actions: {
    asyncAddCount(context, payload) {
      setTimeout(() => {
        context.commit('addCount', payload)
      }, 2000)
    }
  },
  getters: {
    getterCount: state => state.arr.filter(item => item > 5),
    // 快捷访问
    token: state => state.user.token,
    name: state => state.settings.name
  },
  modules: {
    // ! 命名空间
    user: {
      namespaced: true,
      state: {
        token: 11111
      },
      mutations: {
        changeToken(state, payload) {
          state.token = payload
        }
      }
    },
    settings: {
      state: {
        name: '标题'
      }
    }
  }
})

vue组件中使用方式

<template>
  <div id="app">
    <!-- #1 state  -->
    <!-- 方式一 $store.state.count -->
    <h4>{{ $store.state.count }}</h4>
    <!-- 方式二 mapState(['count']) -->
    <h4>{{ count }}</h4>

    <!-- #2 getters  -->
    <!-- 方式一 $store.getters.getterCount -->
    <h4>getter:{{ $store.getters.getterCount }}</h4>
    <!-- 方式二 mapGetters(['getterCount']) -->
    <h4>getter:{{ getterCount }}</h4>

    <!-- #3 mutations -->
    <!-- 方式一 mapMutations(['addCount']) -->
    <div><button @click="addCount(1)">+1</button></div>
    <!-- 方式二 $store.commit('addCount', 100)-->
    <div><button @click="$store.commit('addCount', 100)">+100</button></div>

    <!-- #4 actions -->
    <!-- 方式一 $store.dispatch('asyncAddCount', 2) -->
    <div>
      <button @click="$store.dispatch('asyncAddCount', 2)">async+2</button>
    </div>
    <!-- 方式二 mapActions(['asyncAddCount']) -->
    <div>
      <button @click="asyncAddCount(10)">async+10</button>
    </div>

    <!-- #5 modules 模块化 -->
    <!-- state -->
    <h4>{{ $store.state.user.token }}</h4>
    <h4>{{ $store.state.settings.name }}</h4>
    <h4>{{ token }}</h4>
    <h4>{{ name }}</h4>

    <!-- 采用 namespaced 后的模块调用mutations或actions的方式 -->
    <!-- 方式一 createNamespacedHelpers -->
    <div>
      <button @click="changeToken(10)">修改user的token 1</button>
    </div>
    <!-- 方式二 $store.commit('user/changeToken', 233) -->
    <div>
      <button @click="$store.commit('user/changeToken', 233)">
        修改user的token 2
      </button>
    </div>
    <!-- 方式三 mapMutations(['user/changeToken']) -->
    <div>
      <button @click="changeT(2233)">
        修改user的token 3
      </button>
    </div>
  </div>
</template>

<script>
import {
  mapState,
  mapMutations,
  mapActions,
  mapGetters,
  createNamespacedHelpers
} from 'vuex'
const { mapMutations: userMapMutations } = createNamespacedHelpers('user')
export default {
  name: 'App',
  computed: {
    ...mapState(['count']),
    ...mapGetters(['getterCount', 'name', 'token'])
  },
  methods: {
    ...mapMutations(['addCount', 'user/changeToken']),
    ...mapActions(['asyncAddCount']),
    ...userMapMutations(['changeToken']),
    changeT(val) {
      this['user/changeToken'](val)
    }
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值