美文网首页
Vue状态管理模式「十六」-- Vuex***

Vue状态管理模式「十六」-- Vuex***

作者: loushumei | 来源:发表于2021-01-04 20:25 被阅读0次

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

    一个状态管理实例

    const store = new Vuex.Store({
      state: {
        count: 0,
        todos: [
          { id: 1, text: '...', done: true },
          { id: 2, text: '...', done: false }
        ]
      },
      getters: {
        doneTodos: state => {
          return state.todos.filter(todo => todo.done)
        }
      }
      mutations: {
        increment (state) {
          state.count++
        }
      },
      actions: {
        increment (context) {
          context.commit('increment')
        }
      },
      modules: {
        a: moduleA,
        b: moduleB
      }
    })
    

    state 唯一数据源 Getter 计算属性 Mutation 提交 Action 处理事件异步 Module 模块

    基本概念

    State 唯一数据源

    在 Vue 组件中获得 Vuex 状态

    // 创建一个 Counter 组件
    const Counter = {
      template: `<div>{{ count }}</div>`,
      computed: {
        count () {
          return store.state.count
        }
      }
    }
    

    通过在根实例中注册 store 选项,该 store 实例会注入到根组件下的所有子组件中,且子组件能通过 this.$store 访问到

    const Counter = {
      template: `<div>{{ count }}</div>`,
      computed: {
        count () {
          return this.$store.state.count
        }
      }
    }
    

    mapState 辅助函数 访问

    // 在单独构建的版本中辅助函数为 Vuex.mapState
    import { mapState } from 'vuex'
    
    export default {
      // ...
      computed: mapState({
        // 箭头函数可使代码更简练
        count: state => state.count,
      })
    }
    
    Getter 计算属性

    getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

    mapGetters 辅助函数

    import { mapGetters } from 'vuex'
    
    export default {
      // ...
      computed: {
      // 使用对象展开运算符将 getter 混入 computed 对象中
        ...mapGetters([
          'doneTodosCount',
          'anotherGetter',
          // ...
        ])
      }
    }
    
    Mutation 提交
    • 更改 Vuex 的 store 中的状态的唯一方法

    提交方式:

    // 定义
    mutations: {
      increment (state, n) {
        state.count += n
      }
    }
    // 提交方式
    store.commit('increment')
    

    mapMutations 函数:

    import { mapMutations } from 'vuex'
    
    export default {
      // ...
      methods: {
        ...mapMutations([
          'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
    
          // `mapMutations` 也支持载荷:
          'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
        ]),
        ...mapMutations({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
        })
      }
    }
    
    Action 处理事件异步
    • Action 提交的是 mutation,而不是直接变更状态。
    • Action 可以包含任意异步操作。

    Action 通过 store.dispatch 方法触发:

    store.dispatch('increment')
    

    在组件中分发 Action:mapActions 辅助函数

    import { mapActions } from 'vuex'
    
    export default {
      // ...
      methods: {
        ...mapActions([
          'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
    
          // `mapActions` 也支持载荷:
          'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
        ]),
        ...mapActions({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
        })
      }
    }
    
    Module 模块

    集成多个子模块

    Vuex流程图

    在这里插入图片描述

    注意:
    只能在Action里做异步操作
    Mutations 原子操作,同步数据

    相关文章

      网友评论

          本文标题:Vue状态管理模式「十六」-- Vuex***

          本文链接:https://www.haomeiwen.com/subject/vzfboktx.html