手把手教你学vue-4(vuex)

1.首先明白vuex是作什么用的。

管理统一组件状态state。每一个应用将仅仅包含一个 store 实例。单一状态树让咱们可以直接地定位任一特定的状态片断,在调试的过程当中也能轻易地取得整个当前应用状态的快照。

2.如何实现 vuex有几个对象

  • state =>mapState
  • getters =>mapGetters
  • actions =>mapActions
  • mutations => mapMutations
  • moudle

3.state (注入store)

Vuex 经过 store 选项,提供了一种机制将状态从根组件“注入”到每个子组件中(需调用 Vue.use(Vuex)):html

const app = new Vue({
  el: '#app',
  // 把 store 对象提供给 “store” 选项,这能够把 store 的实例注入全部的子组件
  store,
  components: { Counter },
  template: `
    <div class="app">
      <counter></counter>
    </div>
  `
})

经过在根实例中注册 store 选项,该 store 实例会注入到根组件下的全部子组件中,且子组件能经过 this.$store 访问到
当咱们须要时刻跟踪状态值的变化时,能够经过组件的计算机属性来肯定,而后使用mapState.方法来映射。vue

computed: {
  localComputed () { /* ... */ },
  // 使用对象展开运算符将此对象混入到外部对象中
  ...mapState({
    // ...
  })
}

4.getters (都是方法)

Vuex 容许咱们在 store 中定义“getter”(能够认为是 store 的计算属性)。就像计算属性同样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被从新计算。
  • Getter 接受 state 做为第一个参数
  • mapGetters 辅助函数仅仅是将 store 中的 getter
import { mapGetters } from 'vuex'
export default {
  computed: {
  // 使用对象展开运算符将 getter 混入 computed 对象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}

5. mutation

更改 Vuex 的 store 中的状态的惟一方法是提交 mutation,调用方法 store.commit('increment')
  • 注意点git

    • 使用常量替代 Mutation 事件类型github

      export const SOME_MUTATION = 'SOME_MUTATION'
      // store.js
      import Vuex from 'vuex'
      import { SOME_MUTATION } from './mutation-types'
      
      const store = new Vuex.Store({
        state: { ... },
        mutations: {
          // 咱们可使用 ES2015 风格的计算属性命名功能来使用一个常量做为函数名
          [SOME_MUTATION] (state) {
            // mutate state
          }
        }
      })
  • Mutation 必须是同步函数
  • mapMutationsvuex

    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')`
       })
     }
       }

6.Action

  • Action 提交的是 mutation,而不是直接变动状态。
  • Action 能够包含任意异步操做。
context 不是 store 实例自己
Action 函数接受一个与 store 实例具备相同方法和属性的 context 对象,所以你能够调用 context.commit 提交一个 mutation,或者经过 context.state 和 context.getters 来获取 state 和 getters。
actions: {
 increment ({ commit }) {
   commit('increment')
 }
}
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')`
    })
  }
}

7. Module

概念
Vuex 容许咱们将 store 分割成模块(module)。每一个模块拥有本身的 state、mutation、action、getter.相似redux里面的reducer 针对每一个组件对应的state状态作处理
const moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: { ... },
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态
  • rootState

对于模块内部的 action,局部状态经过 context.state 暴露出来,根节点状态则为 context.rootState。
rootState 能够获取到全部mudule里面的state值redux

const moduleA = {
  // ...
  actions: {
    incrementIfOddOnRootSum ({ state, commit, rootState }) {
      if ((state.count + rootState.count) % 2 === 1) {
        commit('increment')
      }
    }
  }
}

这个仍是要多看官方的demo缓存