1.Vuex
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),让其在各个页面上实现数据的共享包括状态,并且可操作
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
2. vuex使用步骤
安装: npm install vuex -S
显示这个代表成功
创建store模块
创建
index.js
state.js
actions.js
mutations.js
getters.js
等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
3.1:在main.js中加入并使用store实例
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import 'element-ui/lib/theme-chalk/index.css'//2
//process.env.MOCK && require('@/mock')
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'//1
import axios from '@/api/http'
import VueAxios from 'vue-axios'
Vue.use(ElementUI)
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
data(){
return{
Bus:new Vue({
})
}
},
router,
store,
components: {
App
},
template: '<App/>'
})
3.2:store
每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
const store = new Vuex.Store({
state, // 共同维护的一个状态,state里面可以是很多个全局状态
getters, // 获取数据并渲染
actions, // 数据的异步操作
mutations // 处理数据的唯一途径,state的改变或赋值只能在这里
4.案例
actions.js数据异步的操作
export default{
setResturantNameAsync: (context, payload) => {
console.log('aaaa');
setTimeout(()=>{
console.log('bbbb');
//state.resturantName = payload.resturantName;
context.commit('setResturantName', payload); //Action提交的是mutation
},4000);
console.log('cccc');
},
//vuex是不能使用Vue实例的
doAjax: (context, payload) => {
let _this = payload._this;
let url = _this.axios.urls.SYSTEM_USER_DOLOGIN;
_this.axios.post(url, {}).then((response)=> {
console.log('doAjax,,,,');
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。
getters.js
export default {
getResturantName: (state) => {
return state.resturantName;
}
}
mutations.js处理数据和state的赋值
export default {
// type:值:setResturantName
// payload载荷,传参的容器
setResturantName: (state, payload) => {
state.resturantName = payload.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'
})
state.js
export default{//保存数据的容器
resturantName:'飞歌餐馆'
}
VuePage1.vue
<template>
<div>
<h3 style="margin: 60px;">第一个Vuex界面:{{title}}</h3>
<button @click="changeTitle">餐馆易主</button>
<button @click="changeTitleAsync">两个月后餐馆易主</button>
<button @click="doAjax">测试Vuex中使用ajax</button>
</div>
</template>
<script>
export default {
data() {
return {
};
},
methods: {
//设置变量
changeTitle() {
this.$store.commit('setResturantName', {
resturantName: '小李菜刀牛肉馆'
});
},
//异步
changeTitleAsync() {
this.$store.dispatch('setResturantNameAsync', {
resturantName: '小李菜刀羊肉馆'
});
},
doAjax() {
this.$store.dispatch('doAjax', {
_this:this
});
}
},
computed: {
title() {
// return this.$store.state.resturantName;
return this.$store.getters.getResturantName;//获取变量
}
}
}
</script>
<style>
</style>
VuePage2.vue
<template>
<div>
<h3 style="margin: 60px;">第二个Vuex界面:{{title}}</h3>
</div>
</template>
<script>
export default {
data() {
return {
title:''
};
},
created(){
this.title = this.$store.state.resturantName;
}
}
</script>
<style>
</style>
5.总结
1、了解vuex中的各个js文件的用途
2、利用vuex存值
3、利用vuex取值
4、Vuex的异步加载问题
5、Vuex的常用辅助函数
本次的分享就到此结束,感谢您的观看。