什么是Vuex?
官方图解Vuex
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓
库), 让其在各个页面上实现数据的共享包括状态,并且可操作。
1、vue中各个组件之间传值
1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);
2.非父子组件或父子组件
通过数据总数Bus,this. r o o t . root. root.emit(‘事件名’,参数1,参数2,…)
3、非父子组件或父子组件
更好的方式是在vue中使用vuex
父子组件的缺陷:
如果父子的层级关系比较复杂,比如A组件上面有很多层级关系,那么它通信将会非常麻烦。
总线传值的缺陷:
一旦组件多起来的话那么可能会忘记变量对应的事件,不方便组件之间传递的变量的维护。
图解Vuex各组件
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
注:Mutations是同步的set,Action是异步的set
Module是一个大容器它把四个容器集成为了一体向外导出一个对象。
vuex使用步骤
安装
在本地盘符中找到当前的项目点进去用cmd打开执行以下命令即可下载vuex所需的相关依赖。
npm install vuex -S
创建store模块
注意创建的文件名字要和以下一样并且都要是js文件。
Store:
index.js
State.js
Actions.js
Mutations.js
Getters.js
在index.js里面集成四大模块
每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
//1.导入vue核心包
import Vue from 'vue'
//2.导入Vuex的核心包
import Vuex from 'vuex'
//3.导入当前目录下的State的vue对象
import state from './State'
//4.导入当前目录下的Getters的vue对象
import getters from './Getters'
//5.导入当前目录下的actions的vue对象
import actions from './Actions'
//6.导入当前目录下的Mutations的vue对象
import mutations from './Mutations'
//7.将Vuex绑定到Vue对象上
Vue.use(Vuex)
//利用vue对象进行渲染并且向外导出store容器
const store = new Vuex.Store({
state,// 共同维护的一个状态,state里面可以是很多个全局状态
getters, // 获取数据并渲染
actions, // 数据的异步操作
mutations // 处理数据的唯一途径,state的改变或赋值只能在这里
})
export default store
在main.js中导入并使用store实例
import store from './store'
Vuex取值
State.js
状态,即要全局读写的数据
export default {
resturantName:'飞歌餐馆'
}
this.$store.state.resturantName
;//不建议
Getters.js
获取数据并渲染,。
注1:getters将state中定义的值暴露在this. s t o r e . g e t t e r s 对 象 中 , 我 们 可 以 通 过 如 下 代 码 访 问 t h i s . store.getters对象中,我们可以通过如下代码访问 this. store.getters对象中,我们可以通过如下代码访问this.store.getters.resturantName
export default {
getResturantName:(state) => {
return state.resturantName;
}
}
Vuex存值
处理数据的唯一途径,state的改变或赋值只能在这里
Mutations.js
export default {
// type(事件类型): 其值为setResturantName
// payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
setResturantName: (state, payload) => {
state.resturantName = payload.resturantName;
}
}
注1:mutations中方法的调用方式
不能直接调用this. s t o r e . m u t a t i o n s . s e t R e s t u r a n t N a m e ( ′ K F C ′ ) , 必 须 使 用 如 下 方 式 调 用 : t h i s . store.mutations.setResturantName('KFC'),必须使用如下方式调用: this. store.mutations.setResturantName(′KFC′),必须使用如下方式调用:this.store.commit(type,payload);
1、把载荷和type分开提交
store.commit('setResturantName',{
resturantName:'KFC'
})
2、载荷和type写到一起
store.commit({
type: 'setResturantName',
resturantName: 'KFC'
})
注2:一定要记住,Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了。
如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了。
Vuex异步加载
Action类似于 mutation,不同在于:Action提交的是mutation,而不是直接变更状态
-
Action可以包含任意异步操作
-
Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性 但是他们并不是同一个实例,context 包含:
1、 state、2. rootState、3. getters、4. mutations、5. actions 五个属性
注1:actions中方法的调用方式语法如下:
this. s t o r e . d i s p a t c h ( t y p e , p a y l o a d ) ; 例 如 : t h i s . store.dispatch(type,payload); 例如:this. store.dispatch(type,payload);例如:this.store.dispatch(‘setResturantNameByAsync’,{resturantName: ‘啃德鸡2’});
注2:action中提交mutation
context.commit(‘setResturantName’,{resturantName: ‘啃德鸡2’});
注3:VUEX 的 actions 中无法获取到 this 对象
如果要在actions 或者 mutations 中使用this对象。可以在调用的时候把this对象传过去
{resturantName: ‘啃德鸡2’,_this:this}//this就是在调用时的vue实例
Vuex中actions的使用场景
场景1:部门管理中添加或删除了新的部门,员工新增/编辑页面的部门列表需要进行变化
场景2:vuex之使用actions和axios异步初始购物车数据
Action.vue
export default {
setResturantNameByAsync: (context, payload) => {
console.log('xxxx');
setTimeout(() => {
console.log('yyyy');
console.log(payload)
// state.resturantName = payload.resturantName;
context.commit('setResturantName', payload); //Action提交的是mutation
}, 3000)
console.log('zzzz');
//在这个里面是获取不到vue的根实例的
},
doAjax: (context, payload) => {
let _this = payload._this;
let url = _this.axios.urls.SYSTEM_USER_DOLOGIN;
_this.axios.post(url, {}).then((response) => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
}
}
代码示例
基于以上讲解的过程和代码我们先在项目左侧菜单栏增加两个界面配置对应的路由,来具体实现。
项目里创建对应的目录配好路由,定义两个界面主要是为了更好的展示出传值过程。
VuexPage1.vue
<template>
<div>
<h3>页面1:欢迎来到{{msg}}</h3>
<button @click="buy">盘它(同步)</button>
<button @click="buyAsync">搞它(异步)</button>
<button @click="doAjax">搞它(异步)</button>
</div>
</template>
<script>
export default {
data() {
return {
};
}
,
computed: {
msg() {
return this.$store.state.resturantName;
}
},
methods: {
buy() {
this.$store.commit('setResturantName',{
resturantName:'和平精英'
});
},
buyAsync(){
this.$store.dispatch('setResturantNameByAsync',{
resturantName:'qq飞车'
});
},
doAjax(){
this.$store.dispatch('doAjax',{
_this:this
});
}
}
}
</script>
<style>
</style>
VuexPage2.vue
<template>
<div>
<h3>页面2:欢迎来到{{msg}}</h3>
</div>
</template>
<script>
export default {
data() {
return {
};
}
,
computed: {
msg() {
return this.$store.getters.getResturantName;
}
},
}
</script>
<style>
</style>
原始界面
异步请求后台
同步改变状态
异步延迟6秒执行修改状态