介绍
为什么使用vuex
- Vue组件与组件之间的数据是独立的
因为组件的data不是一个对象,而是一个函数,函数返回一个新对象
export default {
data () {
return {} // 因为组件之间数据要保持独立性
}
}
- 父传子-props
- 子传父-$emit-eventbus
- 兄弟组件传值-非关系型组件传值-eventbus
eventbus- 每个组件都要存一个副本
Vuex是专门为vue.js应用程序开发的状态管理模式,它采用集中式存储管理应用的所有的组件状态,并以相应的规则保证状态以一种可预测的方式变化
简单来说Vuex是一种采用集中式管理组件依赖的共享数据工具,可以解决不同组件数据共享问题
Vuex的过程
- 修改state状态必须通过mutations
- mutations只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行
- 执行异步代码,要通过actions,然后将数据提交给mutations才可以完成
- state的状态即共享数据可以在组件中引用
- 组件中可以调用action
初始化Vuex
1.下载vuex包
安装vuex到运行时依赖
$ npm i vuex@3.6.2
$ yarn add vuex@3.6.2
2.引入Vuex包
import Vuex from 'vuex' // 引入Vuex包
3.使用Vue.use全局注册Vuex
Vue.use(Vuex) // Vue.use - 会调用传入对象里面的install方法- 将Vue自身传入
4.实例化仓库
const store = new Vuex.Store({
// 配置项
})
5.在根实例配置 store 选项指向 store 实例对象
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(vuex)
const store = new Vuex.Store({})
new Vue({
el: '#app',
store
})
Vuex - state
state是放置所有公共状态的属性,如果你有一个公共状态数据,你只需要定义在state对象中
// 初始化vuex对象
const store = new Vuex.Store({
state: {
// 管理数据
count: 0
}
})
如何获取管理数据
原始形式-插值表达式
App.vue
组件中可以使用 this.$store 获取到vuex中的store对象实例,可通过state属性属性获取count, 如下
<div> state的数据:{{ $store.state.count }}</div>
计算属性
将state属性定义在计算属性中
// 把state中数据,定义在组件内的计算属性中
computed: {
count () {
return this.$store.state.count
}
}
<div> state的数据:{{ count }}</div>
辅助函数 - mapState
mapState是辅助函数,帮助我们把store中的数据映射到组件的计算属性中,它属于一种方便用法
第一步:导入mapState
import { mapState } from 'vuex'
第二步:采用数组形式引入state属性
mapState(['count'])
第三步:利用延展运算符将导出的状态映射给计算属性
computed: {
...mapState(['count'])
}
computed: {
...mapState(['count'])
}
Vuex - mutations
state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成数据快照
数据快照:一次mutations的执行,立刻得到一种视图,因为是立刻,所以必须是同步
同步代码和异步代码相遇,先将异步线程挂起,同步主线执行完毕,再执行异步
定义mutations
const store = new Vuex.Store({
state: {
count: 0
},
// 定义mutations
mutations: {
}
})
格式说明
mutations是一个对象,对象中存放修改state的方法
mutations: {
// 方法里参数 第一个参数是当前store的state属性
// payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
addCount (state) {
state.count += 1
}
},
如何在组件中调用mutations
this.$store 等价于 创建的store实例
原始形式-$store
新建组件child-a.vue,内容为一个button按钮,点击按钮调用mutations
<template>
<button @click="addCount">+1</button>
</template>
<script>
export default {
methods: {
// 调用方法
addCount () {
// 调用store中的mutations 提交给muations
// commit('muations名称', 2)
this.$store.commit('addCount', 10) // 直接调用mutations
}
}
}
</script>
带参数的传递
addCount (state, payload) {
state.count += payload
}
this.$store.commit('addCount', 10)
辅助函数 - mapMutations
mapMutations和mapState很像,它把位于mutations中的方法提取了出来,我们可以将它导入
import { mapMutations } from 'vuex'
methods: {
...mapMutations(['addCount'])
}
此时,就可以直接通过this.addCount调用了
<button @click="addCount(100)"> +100 </button>
但是请注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中
Vuex - actions
state是存放数据的,mutations是同步更新数据,actions则负责进行异步操作
定义actions
actions: {
// 异步更新- context等价于 store 等价于this.$store.commit()
getAsyncCount(context) {
setTimeout(() => {
context.commit("addCount", ~~(Math.random() * 100));
}, 1000);
},
},
原始调用 - $store
addAsyncCount () {
this.$store.dispatch('getAsyncCount')
}
传参调用
addAsyncCount () {
this.$store.dispatch('getAsyncCount')
}
辅助函数 -mapActions
actions也有辅助函数,可以将action导入到组件中
import { mapActions } from 'vuex'
methods: {
...mapActions(['getAsyncCount'])
}
直接通过 this.方法就可以调用
<button @click="getAsyncCount(111)">+异步</button>
注意:调用Vuex中的action返回的是一个promise对象,如果想要获取它的返回值,你应该这样做
this.$store.dispatch("getAsyncCount").then(result => {
// 获取结果
})
// 又或者使用aynsc/await配合使用
const result = this.$store.dispatch("getAsyncCount")