Vuex中的各个js文件的用途
变量传值的演变形式
这是之前博客中出现的两种vue的传值方式
图解Vuex
举个例子,有A和B两个组件,当A传值到B时就可以将变量定义到state中去,从而解决组件过多导致忘记变量的情况,然后通过getters和mutations进行数据的拿取
-
vue中各个组件之间传值(第一张图)
1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);2.非父子组件或父子组件
通过数据总数Bus,this. r o o t . root. root.emit(‘事件名’,参数1,参数2,…)3.非父子组件或父子组件(第二张图)
更好的方式是在vue中使用vuex方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据 -
Vuex
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),
让其在各个页面上实现数据的共享包括状态,并且可操作Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
Module就是将vuex分成了State,Getters,Mutations,Actions四个模块,故图中没有出现
vuex的核心概念:store、state、getters、mutations、actions
store
每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
const store = new Vuex.Store({
state, // 共同维护的一个状态,state里面可以是很多个全局状态
getters, // 获取数据并渲染
actions, // 数据的异步操作
mutations // 处理数据的唯一途径,state的改变或赋值只能在这里
})
vuex使用步骤
1 安装
npm install vuex -S
2 创建store模块,分别维护state/actions/mutations/getters
store
index.js (Module)
state.js
actions.js
mutations.js
getters.js
注:名字请注意与第三步保持一致
3 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块
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,
getters,
actions,
mutations
})
export default store
4 在main.js中导入并使用store实例
import store from './store'
new Vue({
el: '#app',
router,
store, //在main.js中导入store实例
components: {
App
},
template: '<App/>',
data: {
//自定义的事件总线对象,用于父子组件的通信
Bus: new Vue()
}
})
Vuex取值
state.js(保存数据的容器)
export default {
//状态,即要全局读写的数据
resturantName: '飞歌餐馆'
}
getters.js
export default {
// 获取数据并渲染,
getResturantName: (state) => {
return state.resturantName;
}
}
注
注1:getters将state中定义的值暴露在this.$store.getters对象中,我们可以通过如下代码访问
this.$store.getters.resturantName
注2:state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态,如下:
computed: {
resturantName: function() {
return this.$store.getters.resturantName;
}
}
Vuex存值
Mutations.js
// 处理数据的唯一途径,state的改变或赋值只能在这里
export default {
// state 存储变量的容器
// payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
setResturantName: (state, payload) => {
state.resturantName = payload.resturantName;
}
}
注1:mutations中方法的调用方式
不能直接调用this.$store.mutations.setResturantName('KFC'),必须使用如下方式调用:
this.$store.commit(type,payload);
案例(同步改变)
VuexPage1.vue
<template>
<div>
<h3>页面1,欢迎来到{{msg}}</h3>
<button @click="buy">盘它</button>
</div>
</template>
<script>
export default{
data(){
return {
};
},
computed:{
msg(){
return this.$store.state.resturantName;
}
},
methods:{
buy(){
this.$store.commit('setResturantName',{
resturantName:'KFC'
});
}
}
}
</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>
这是为点击按钮的情况,也就是还没进行取值
执行按钮后,也就是执行取值操作
飞歌餐馆瞬间变成KFC
案例(异步改变)
好处:两件事情能同时进行,在项目中能够让tomcat同步进行节约时间,用空间换取时间
因为JAVA虚拟机中默认空间只有512MB,如果多线程跑多项目可能会崩溃
Actions.js
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');
}
}
VuexPage1.vue
<template>
<div>
<h3>页面1,欢迎来到{{msg}}</h3>
<button @click="buy">盘它(同步)</button>
<button @click="buyAsync">盘它(异步)</button>
</div>
</template>
<script>
export default {
data() {
return {
};
},
computed: {
msg() {
return this.$store.state.resturantName;
}
},
methods: {
buy() {
this.$store.commit('setResturantName', {
resturantName: 'KFC'
});
},
buyAsync() {
this.$store.dispatch('setResturantNameByAsync', {
resturantName: 'KFC'
});
}
}
}
</script>
<style>
</style>
点击异步,飞歌餐馆将在3秒后变成KFC
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.
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异步初始购物车数据
在vuex中使用ajax
Actions.js
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');
},
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: 'KFC'
});
},
buyAsync() {
this.$store.dispatch('setResturantNameByAsync', {
resturantName: 'KFC'
});
},
doAjax() {
this.$store.dispatch('doAjax', {
_this:this
});
}
}
}
</script>
<style>
</style>