Vuex介绍
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库), 让其在各个页面上实现数据的共享包括状态,并且可操作
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
vue中各个组件之间传值
-
父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…); -
非父子组件或父子组件
通过数据总数Bus,this. r o o t . root. root.emit(‘事件名’,参数1,参数2,…) -
非父子组件或父子组件
更好的方式是在vue中使用vuex
vuex使用步骤
1 安装
打开cmd窗口,找到项目路径运用以下指令
npm install vuex -S
2 在src下创建store模块,分别维护state/actions/mutations/getters
注意创建的文件名字要和以下一样并且都要是js文件
index.js代码:
import Vue from 'vue'
import Vuex from 'vuex'
import state from './State'
import getters from './Getters'
import actions from './Actions'
import mutations from './Mutations'
Vue.use(Vuex)
const store = new Vuex.Store({
state, // 共同维护的一个状态,state里面可以是很多个全局状态
getters, // 获取数据并渲染
actions,// 数据的异步操作
mutations// 处理数据的唯一途径,state的改变或赋值只能在这里
})
export default store
3 在main.js中导入并使用store实例
Vuex同步取值
State.js :
状态,即要全局读写的数据
const state = {
resturantName:‘飞歌餐馆’ //定义一个变量
};
this.$store.state.resturantName;//不建议
Getters.js
获取数据并渲染,
export default {//取值界面
GetResturantName: (state) => {
return state.resturantName;
}
}
注1 :getters将state中定义的值暴露在this.$store.getters对象中,我们可以通过如下代码访问 this.$store.getters.resturantName
mutations中方法的调用方式:
不能直接调用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'
})
Vuex异步加载
Actions.js:
export default { //写所有异步操作
setResturantNameByAsync: (context, payload) => { //context是有一个大容器
console.log('xxxx');
setTimeout(() => {
console.log('yyyy');
console.log(payload)
// state.resturantName = payload.resturantName;
context.commit('setResturantName', payload); //Action提交的是mutation
}, 6000) //延迟3秒后执行setTimeout里面代码内容
console.log('zzzz');
//在这里面是获取不到this的(Vue的根实例)
},
doAjax: (context, payload) => {
let _this = payload._this //接收this
let url = _this.axios.urls.SYSTEM_USER_DOLOGIN;
_this.axios.post(url, {}).then((response) => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
}
}
Action类似于 mutation,不同在于:
1.Action提交的是mutation,而不是直接变更状态
2.Action可以包含任意异步操作
3.Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性
但是他们并不是同一个实例,context 包含:
1. state、2. rootState、3. getters、4. mutations、5. actions 五个属性
所以在这里可以使用 context.commit 来提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。
注1:actions中方法的调用方式语法如下:
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实例
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>