Vuex 是一个专门为 vue.js 应用程序开发的状态管理模式
- 这个状态咱们能够理解为在 data 中的属性,须要共享给其余组件使用的部分
- 也就是说,咱们须要共享的数据,可使用 vuex 进行统一集中式的管理
https://gitee.com/jiangliyue/...
<br/>vue
- state:存储状态(变量)
- getters:对数据获取以前的再次编译,能够理解为 state 的计算属性,咱们在组件中使用 $store.getters.fun() 调用
- mutations:修改状态,而且是同步的。在组件中使用 $store.commit('操做名',params) 调用
- actions:异步操做。在组件中使用 $store.dispatch('操做名') 调用
- modules:store 的子模块,为了开发大型项目,方便状态管理而使用的,使用方法和上面同样
// 常见vuex初始化配置 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { msg: 'hello ', username: '' }, getters: {}, mutations: {}, actions: {}, modules: {} })
<br/>git
将上面初始化的 Vuex 对象引入到 main.js 文件中
// 引入store import store from '@/store/index' new Vue({ store, render: h => h(App) }).$mount('#app')
在组件中使用
<template> <div class="hello"> <h3>{{$store.state.msg}}</h3> </div> </template> <script> export default { name: 'home', created() { console.log(this.$store.state) } } </script>
Vuex 提供了 mutations 和 actions 两种方法来操做 state
定义 mutations 对象里的方法,方法里面的参数,第一个默认为 state,第二个为自定义传入参数。
/** * mutations 里面放置的是咱们操做state对象属性的方法 */ const mutations = { setMsg(state, name) { state.msg = 'hellow' + name } } export default new Vuex.Store({ state, mutations })
组件中使用 Vuex 提供的 commit 方法调用 mutations 中咱们自定义的方法
created() { this.setSayMsg('your Name') }, methods: { setSayMsg(name) { this.$store.commit('setMsg',name) } }
效果如何你们自行实验哈~es6
定义 actions 对象里的方法,方法里面的参数,第一个是context,它是一个和 store 对象具备相同对象属性的参数,第二个为自定义传入参数。
一般当咱们须要修改多个状态或者说调用多个 mutations 里的方法时会用到 actions
/** * actions 里面放置的是咱们调用store对象的方法 */ const actions = { // 常规写法 // actionsSetMsg(context, name) { // context.commit('setMsg', name) // } // 简化写法 actionsSetMsg({ commit }, name) { commit('setMsg', name) } } export default new Vuex.Store({ state, actions })
组件中使用 Vuex 提供的 dispatch 方法调用 actions 中咱们自定义的方法
created() { this.actionsSetSayMsg('your Name') }, methods: { actionsSetSayMsg(name) { this.$store.dispatch('actionsSetMsg',name) } }
定义 getters 对象里的方法
const getters = { getMsg(state) { return state.msg } } export default new Vuex.Store({ state, getters })
组件中使用
computed: { msg() { return this.$store.getters.getMsg } }
看到这里若是上面的用法都能理解,那恭喜你的 Vuex 已经学的很好了!vuex
mapState
mapMutations
mapActions
mapGetters
- 须要明确的是,这些方法只是方便咱们书写,重点仍是上面的基础部分
- 这部分用到了 es6 的扩展运算符,不熟悉的同窗自行百度吧,仍是蛮有用的
用法仍是看下面代码直接点,主要记住2点
- 引入都用到扩展运算符,即3个点 '...' ,方式则为下面 2种模板任选一种
...mapState({ 你在该页面想用的变量名: '你state对象里定义的属性或方法名' }) // 或者传入一个数组,此时当前组件调用的变量名与state中定义的方法名一致 ...mapState(['你state对象里定义的属性或方法名'])
- 另外记住 ...mapState({...}) 和 ...mapGetters({...}) 放在 computed 计算属性里, ...mapMutations({...}) 和 ...mapActions({...})放在methods 方法属性里就能够了
https://gitee.com/jiangliyue/...
<template> <div class="hello"> <h1>Msg: {{ $store.state.msg }}</h1> <h1>Your Name: {{ username }}</h1> <h1>mapState Your Name: {{ mapStateName }}</h1> <h1>mapGetters Your Name: {{ mapGettersName }}</h1> <h1>User Name: <input type="text" v-model="name" /></h1> <h1> <button @click="save">保存用户名</button> <button @click="mapSave(name)">map 模式</button> </h1> <h1> <button @click="saveAndUpdate">保存用户名并更新语句</button> <button @click="mapSaveAndUpdate(name)">map 模式</button> </h1> </div> </template> <script> import { mapState, mapMutations, mapActions, mapGetters } from 'vuex' export default { name: 'HelloWorld', data() { return { name: '' } }, computed: { username() { return this.$store.getters.getUserName }, ...mapState({ mapStateName: 'username' }), ...mapGetters({ mapGettersName: 'getUserName' }) }, methods: { save() { // // 粗暴写法(不推荐,主要不利于维护) // this.$store.state.username = this.name // 同步写法 使用 commit 调用 mutations this.$store.commit('setUserName', this.name) }, saveAndUpdate() { // 异步写法 使用 dispatch 调用 actions // 通常用于须要依次改变多个状态的流程 this.$store.dispatch('saveAndUpdate', this.name) }, // 使用 mapMutations, mapActions 来简化代码 // 传入数组生成一个 this.setUserName(data) 的方法 // ...mapMutations(['setUserName']), // 传入对象 可从新定义方法名 生成一个 this.mapSave(data) 的方法 ...mapMutations({ mapSave: 'setUserName' }), // 同理 actions 也能够传入数组一次性生成多个方法 ...mapActions({ mapSaveAndUpdate: 'saveAndUpdate' }) } } </script>