当前位置: 代码迷 >> 综合 >> Vuex 常用 API 及其使用方式
  详细解决方案

Vuex 常用 API 及其使用方式

热度:74   发布时间:2024-03-08 04:47:01.0

Vuex 是一种状态管理模式。state:驱动应用的数据源;view:以声明方式将 state 映射到视图;actions: 响应在 view 上的用户输入导致的状态变化。

1.State: 提供一个响应式数据

this.$store.state.xxx / mapState 取值

2.Getter:借助 Vue 的计算属性 computed 来实现缓存

this.$store.getters.xxx / mapGetters 取值

3.Mutation:更改 state 方法

this.$store.commit('xxx') / mapMutations 赋值

4.Action:触发 mutation 方法

this.$store.dispatch('xxx') / mapActions 赋值

5.Module:Vue.set 动态添加 state 到响应式数据中

(1)开启命名空间 namespaced: true

(2)嵌套模块不用过深,尽量扁平化

(3)灵活应用 createNamespacedHelpers