Vuex插件
Vuex 是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。
使用 Vuex 统一管理状态的好处
① 能够在 vuex 中集中管理共享的数据,易于开发和后期维护
② 能够高效地实现组件之间的数据共享,提高开发效率
③ 存储在 vuex 中的数据都是响应式的,能够实时保持数据与页面的同步
一般情况下,只有组件之间共享的数据,才有必要存储到 vuex 中;
vuex的基本使用
1.安装vuex依赖包
npm install vuex --save
2.引入vuex
import Vuex from 'vuex';
Vue.use(Vuex);
3.创建store对象
const store = new Vuex.Store({
//state中存放的就是全局共享的数据
state:{
count:0
}
});
4.将store对象挂载到vue实例中
new Vue({
el: '#app',
render: h => h(app),
router,
// 将创建的共享数据对象,挂载到 Vue 实例中
// 所有的组件,就可以直接从 store 中获取全局的数据了
store
})
Vuex的核心概念
Vuex的工作原理
首先vue组件会通过this.store.state或者mapState()直接从State中获取数据。或者通过this.store.getters或者mapGetters()从getters中获取数据。getters是计算属性数据 ,可以读取State中的数据。
vue组件会通过$store.dispatch()或者mapActions触发actions。之后,actions通过commit触发mutations。而mutations会直接更新State中的数据
Vuex 中的主要核心概念如下:
State
Mutation
Action
Getter
State
State 提供唯一的公共数据源,所有共享的数据都要统一放到 Store 的 State 中进行存储。
// 创建store数据源,提供唯一公共数据
const store = new Vuex.Store({
state: { count: 0 }
})
组件访问 State 中数据的第一种方式:
this.$store.state.count;//this.$store.state.全局数据名称
组件访问 State 中数据的第二种方式:
//1. 从 vuex 中按需导入 mapState 函数
import {mapState} from 'vuex';
//2.通过刚才导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的 computed 计算属性,这样在组件就可以直接通过计算属性访问全局数据
computed:{
//数组参数,‘count’既作为this.$store.state的取值key值,也作为计算属性中的函数名
...mapState(['count']);
//对象参数,属性名作为计算属性中的函数名,属性值作为this.$store.state的取值key值
...mapState({count:'count'})
}
Mutation
Mutation 用于变更 Store中 的数据。
① 只能通过 mutation 变更 Store 数据,不可以直接操作 Store 中的数据。
② 通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化。
//1.在store对象中定义Mutation
const store = new Vuex.Store({
state: { count: 0 } ,
mutations:{
//state默认传入的第一个参数,state对象用来操作数据
add(state,step){
//第一个形参永远都是state也就是$state对象
//第二个形参是调用add时传递的参数
//最多只能接收两个参数,如果要传递多个参数可以用对象或者数组传递
state.count+=step;
}
}
})
触发 mutations 的第一种方式
//1.在组件中通过this.$store.commit('方法名',参数)触发mutation中的方法,进而修改state中的数据
methods:{
Add(){
this.$store.commit('add',10);
}
}
触发 mutations 的第二种方式:
// 1. 从 vuex 中按需导入 mapMutations 函数
import {mapMutations} from 'vuex'
//2.通过刚才导入的 mapMutations 函数,将需要的 mutations 函数,映射为当前组件的 methods 方法,这样在组件中就可以直接调用,模版中绑定改方法时必须传递所需参数,否则会讲事件对象作为参数传递
methods:{
//同样可以传递对象参数或者数组参数
...mapMutations(['add']);
...mapMutations({add:'add'})
}
Action
Action 用于处理异步任务。
如果通过异步操作变更数据,必须通过 Action,而不能使用 Mutation,在mutations中通过异步操作会导致vue调试器的显示出错。
但是在 Action 中还是要通过触发 Mutation 的方式间接变更数据。
//1.在store对象中定义action
const store = new Vuex.Store({
state: { count: 0 } ,
mutations:{
add(state,step){
//第一个形参永远都是state也就是$state对象
//第二个形参是调用add时传递的参数
//最多只能接收两个参数,如果要传递多个参数可以用对象或者数组传递
state.count+=step;
}
},
actions:{
//context默认传入的第一个参数,用来触发mutations中的方法,context中可以获取到store的一些数据,例如state,commit,dispath等
addAsync(context,step){
setTimeout(() => {
context.commit('add',step)
}, 1000)
}
}
})
触发Action的第一种方式
// 触发 Action ,在组件中通过this.$store.dispatch('方法名',参数),和mutations中的方法一样最多只能接收两个参数,如果要传递多个参数可以用对象或者数组传递
methods: {
handle() {
// 在调用 dispatch 函数,
// 触发 actions 时携带参数
this.$store.dispatch('addNAsync', 5)
}
}
触发Action的第二种方式
// 1. 从 vuex 中按需导入 mapActions 函数
import { mapActions } from 'vuex'
//2.将指定的 actions 函数,映射为当前组件的 methods 函数 ,这样就可以在组件中直接调用
methods: {
//同样可以传递对象参数或者数组参数
...mapActions(['addASync', 'addNASync'])
...mapActions({
addASync,'addASync'
})
}
Getter
Getter 用于对 Store 中的数据进行加工处理形成新的数据。
① Getter 可以对 Store 中已有的数据加工处理之后形成新的数据,类似 Vue 的计算属性。
② Store 中数据发生变化,Getter 的数据也会跟着变化
// 定义 Getter
const store = new Vuex.Store({
state: {
count: 0
},
getters: {
showNum: state => {
return '当前最新的数量是【'+ state.count +'】'
}
}
})
Getter
使用 getters 的第一种方式:
this.$store.getters.名称
使用 getters 的第二种方式:
import { mapGetters } from 'vuex'
computed: {
...mapGetters(['showNum'])
...mapGetters({
showNum:'showNum'
})
}
Vuex模块化
const a = {
//开启命名空间,在组件中使用时才可以通过Vuex的函数映射状态时才可以通过模块名获取到模块中的state,actions,mutations,getters等数据
namespaced:true,
state:{},
mutations:{},
actions:{},
getters:{}
}
const b = {
namespaced:true,
state:{},
mutations:{},
actions:{},
getters:{}
}
const store = new Vuex.Store({
modules:{
aAbout:a,
bAbout:b,
}
});
//在组件中获取
this.$store.aAbout.state.xxx
this.$store.commit('aAbout/xxx',params)
this.$store.dispath('aAbout/xxx',params)
...mapState('aAbout',['xxx','xxx'])
...mapMutations('aAbout',['xxx','xxx'])
...mapActions('aAbout',['xxx','xxx'])
...mapGetters('aAbout',['xxx','xxx'])