【Vue】Vuex汇总

Vuex

Vuex 概述

  1. 组件之间共享数据的方式(小范围内数据共享)
  • 父传子:v-bind 属性绑定
  • 子传父:v-on 事件绑定
  • 兄弟组件:EventBus
    • $on 接收数据的那个组件
    • $emit 发送数据的那个组件
  1. Vuex 是什么
  • Vuex 是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享,是一种状态管理模式
  1. 使用 Vuex 统一管理状态的好处
  • 能够在 Vuex 中几种管理共享的数据,易于开发和后期维护
  • 能够高效地实现组件之间的数据共享,提高开发效率
  • 存储在 Vuex 中的数据都是响应式的,能够实时保持数据与页面的同步
  1. 什么样的数据适合存储到 Vuex 中
  • 一般情况下,只有组件之间共享的数据,才有必要存储到 Vuex 中,对于组件中的私有数据,依旧存储在组件自身的 data 中即可

Vuex 的基本使用

  1. 安装 Vuex 依赖包
npm i vuex -S
  1. 导入 vuex 包
import Vuex from 'vuex
Vue.use(Vuex)'
  1. 创建 store 对象
const store = new Vuex.Store({
  // state 中存放的就是全局共享的数据
  state:{ count: 0 }
})
  1. 将 store 对象挂载到 vue 实例中
new Vue({
  el:'#app',
  render:h => h(app),
  router,
  // 将创建的共享数据对象,挂载到Vue实例中
  // 所有的组件,就可以直接从store中获取全局的数据了
  store
})

Vuex 的核心概念

  • State

    • State 提供唯一的公共数据源,所有共享的数据都要统一放到 Store 的 State 中进行存储
    • 注意:只有 mutations 中定义的函数才有权利修改 State 中的数据
    // 创建store数据源,提供唯一公共数据
    const store = new Vuex.Store({
      state: { count:0 }
    })
    
    • 组件访问 State 中数据的第一种方式
    this.$store.state.全局数据名称
    
    • 组件访问 State 中数据的第二种方式
    // 1. 从Vuex中按需导入 mapState 函数
    import { mapState } from 'vuex'
    

    通过刚才导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的 computed 计算属性

    // 2. 将全局数据,映射为当前组件的计算属性
    computed: {
      ...mapState(['count'])
    }
    
  • Mutation

    • Mutation 用于变更 Store 中的数据(Vuex 并不提倡直接操作全局数据,推荐使用 Mutation 修改全局数据)

    • 注意:只有 mutations 中定义的函数才有权利修改 State 中的数据

      1. 只能通过 mutation 变更 Store 中的数据,不可以直接操作 Store 中的数据,只有 mutation 中的函数有权修改 state 中的数据。
      2. 通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化。
      // store中定义Mutation
      const store = new Vuex.store({
        state: {
          count:0
        },
        mutations: {
          // mutation中的方法第一个参数永远为自身的state
          add(state){
            // 变更状态
            state.count++
          }
        }
      })
      
      // 组件中触发mutation
      // 触发mutation的第一种方式
      methods: {
        handle() {
          // commit 的作用就是调用某个mutation函数
          this.$store.commit('add')
        }
      }
      
      1. 可以在触发 mutations 时传递参数:
      // 定义Mutation
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        mutation: {
          addN(state,step) {
            // 变更状态
            state,count += step
          }
        }
      })
      
      // 触发mutation
      methods: {
        handle(){
          // 在调用commit函数,触发mutations时携带参数
          this.$store.commit('add',2)
        }
      }
      
      1. this.$store.commit()是触发 mutations 的第一种方式,触发 mutations 的第二种方式:
      // 1. 从vuex中按需导入mapMutations函数
      import { mapMutations } from 'vuex'
      

      通过刚才导入的 mapMutations 函数,将需要的 mutations 函数映射为当前组件的 methods 方法:

      // 2. 将制定的mutations函数映射为当前组件的methods函数
      methods: {
        ...mapMutations(['add','addN'])
      }
      
      1. 注意:不要在 mutations 中使用异步操作,应使用 Action
  • Action

    • Action 用于处理异步任务,如果通过异步操作变更数据,必须通过 Action,而不能使用 Mutation,但是在 Action 中还是要通过触发 Mutation 的方式间接变更数据。
    • 在 action 中不能直接修改 state 中的数据,如果要修改 state 中的数据,必须通过 context.commit()触发某个 mutation 才行(可以将 context 认为 new 出来的 store 实例对象)
    const store = new Vuex.Store({
      // ...省略其他代码
      mutations:{
        add(state){
          state.count++
        }
      },
      // 定义Action
      actions: {
        addAsync(context){
          setTimeout(() => {
            context.commit('add')
          },1000)
        }
      }
    })
    
    // 组件中触发Action
    methods:{
      handle(){
        // 触发actions的第一种方式
        // 这里的dispatch函数,专门用来触发action
        this.$store.dispatch('addAsync')
      }
    }
    
    • this.$store.dispatch()是触发 actions 的第一种方式,触发 actions 的第二种方式:
    // 1. 从vuex中按需导入mapActions函数
    import { mapActions } from 'vuex'
    

    通过刚才导入的 mapActions 函数,将需要的 actions 函数,映射为当前组件的 methods 方法:

    // 2. 将指定的actions函数,映射为当前组件的methods函数
    methods:{
      ...mapActions(['addAsync'])
    }
    
  • Getter

    • Getter 用于对 Store 中的数据进行加工处理形成新的数据(并不会修改 Store 中的源数据,只是对Store中的数据起到一个包装的作用)
      1. Getter 可以对 Store 中已有的数据加工处理之后形成新的数据,类似 Vue 的计算属性。
      2. Store 中数据发生变化,Getter 的数据也会跟着变化。
      // 定义Getter
      const store = new Vuex.Store({
        state: {
          count: 0
        },
        getters: {
          showNum: state => {
            return '当前最新的数量是【'+ state.count +'】'
          }
        }
      })
      
    • 使用 getters 的第一种方式:
    this.$store.getters.名称
    
    • 使用 getters 的第二种方式:
    import { mapGetters } from 'vuex'
    
    computed: {
      ...mapGetters(['showNum'])
    }
    

使用 Vuex 的好处

  1. 多层嵌套的组件、兄弟组件间的状态会更好管理维护。
  2. 缓存一些当前要使用请求远程或本地的数据集(刷新后会自己销毁)。
  3. 有了第二条,就可以减少向服务器的请求,节省资源。如果你的用户足够多,那么每多出一个请求,对公司来说,都是一大笔钱。
  4. 对开发者来说,如果你的项目足够复杂,团队的规模也不仅是一个人,数据集中处理更利于程序的稳定和维护。

猜你喜欢

转载自www.cnblogs.com/AAABingBing/p/12726473.html