vuex 学习笔记

37 阅读2分钟

1. vuex 介绍

1.1 组件之间共享数据的方式

  • 父向子传值:v-bind 属性绑定

  • 子向父传值:v-on 事件绑定

  • 兄弟组件之间共享数据:EventBus

    • $on 接受数据的组件

    • $emit 发送数据的组件

1.2 vuex 是什么

Vuex 是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。

1.3 vuex 统一管理状态的好处

  1. 能够在 vuex 中集中管理共享的数据,易于开发和后期维护

  2. 能够高效地实现组件之间的数据共享,提高开发效率

  3. 存储在 vuex 中的数据都是响应式地,能够实时保持数据与页面的同步

2. 基本使用

2.1 安装

npm install vuex --save

2.2 导入

import Vuex from 'vuex'
Vue.use(Vuex)

2.3 创建 store 对象

const store = new Vuex.Store({
    // state 中存放的就是全局共享的数据
    state: {
        count: 0
    }
})

2.4 将 store 对象挂载到 vue 实例中

new Vue({
    el: '#app',
    render: h => h(app),
    router,
    store
})

3. 核心概念

Vuex 中的主要核心概念如下:

  • state:提供唯一公共数据源,所有共享的数据都要统一放到 store 的 state 中进行存储。

  • mutation:用于变更 store 中的数据。

  • action:用于处理异步任务。

  • getter:用于对 store 中的数据进行加工处理形成新的数据。

注意在编写代码的时候, mutation、action、getter 都应当是复数形式:mutations、actions、getters

3.1 state

state 提供唯一公共数据源,所有共享的数据都要统一放到 store 的 state 中进行存储。

const store = new Vuex.Store({
    state: {
        count: 0    
    }
})

组件访问 state 中数据的第一种方式:

this.$store.state.全局数据名称

组件访问 state 中数据的第二种方式:

// 1. 从 vuex 中按需导入 mapState 函数
import { mapState } from 'vuex'
// 2. 将全局数据映射为当前组件的 computed 计算属性
computed: {    
    ...mapState(['count'])
}

3.2 mutation

mutation 用于变更 store 中的数据。

  1. 只能通过 mutation 变更 store 数据,不可以直接操作 store 中的数据。

  2. 通过这种方式可以集中监控所有数据的变化。

    const store = new Vuex.Store({ state: { count: 0 }, // 在 mutations 里写处理数据变更逻辑的函数 mutations: { add(state) { // 数据变更 state.count++ } } })

触发 mutations 的第一种方式:

methods: {
    handle1() {
        this.$store.commit('add')
    }
}

触发 mutations 的第二种方式:

// 1. 从 vuex 中按需导入 mapMutations 函数
import { mapMutations } from 'vuex'
// 2. 将 mutations 函数映射为当前组件的 methods 方法
methods: {
    ...mapMutations(['add', 'addN'])
}
// 3. 使用
this.add()
// addN 见下面的操作
this.addN()

还可以在触发 mutations 时传递参数:

const store = new Vuex.Store({
    state: {        count: 0    },
    mutations: {
        addN(state, step) {
            state.count += step
        }
    }
})

触发同样有两种方式,第一种:

let step = 1this.$store.commit('addN', step)

第二种:

let step = 1this.addN(step)

3.3 action

actions 用于处理异步任务。

如果通过异步操作变更数据,必须通过 action,而不能使用 mutation,但是在 action 中还是要通过触发 mutation 的方式间接变更数据。

const store = new Vuex.Store({
    // ... 省略其它代码
    mutations: {
        addN(state) {
            state.count ++
        }
    },
    actions: {
        addAsync(context) {
            // 通过 context 调用 mutations 中的数据
            setTimeout(() => {
                context.commit('addN')
            }, 1000)
        }
    }
})

触发 mutations,第一种方式:

this.$store.dispatch('addAsync')

触发 mutations,第二种方式:

// 1. 从 vuex 中按需导入 mapActons 函数
import { mapActions } from 'vuex'
// 2. 将指定的 action 函数,映射为当前组建的 methods 方法
methods: {
    ...mapActions(['addASync', 'addNASync'])
}

携带参数:

const store = new Vuex.Store({
    // ... 省略其它代码
    mutations: {
        addN(state, step) {
            state.count ++
        }
    },
    actions: {
        addNAsync(context, step) {
            // 通过 context 调用 mutations 中的数据
            setTimeout(() => {
                context.commit('addN', step)
            }, 1000)
        }
    }
})

调用:

this.$store.dispatch('addNAsync', 5)

3.4 getter

getter 用于对 store 中的数据进行加工处理形成新的数据。

  1. getter 可以对 store 中已有的数据加工处理之后形成新的数据,类似于 vue 的计算属性。

  2. store 中的数据发生变化,getter 的数据也会跟着变化。

    const store = new Vuex.Store({ state: { count: 0 }, getters: { showNum: state => { return '当前最新的数量是【' + state.count + '】' } } })

使用 getters 的第一种方式:

this.$store.getters.名称

使用 getters 的第二种方式:

// 1. 按需导入 mapGetters 方法
import { mapGetters } from 'vuex'
// 2. 映射为计算属性
computed: {
    ...mapGetters(['showNum'])
}