vuex
1、 vue中各个组件之间传值
1.1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);
1.2.非父子组件或父子组件
通过数据总数Bus,this.
r
o
o
t
.
root.
root.emit(‘事件名’,参数1,参数2,…)
1.3.非父子组件或父子组件
更好的方式是在vue中使用vuex
方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据
2. Vuex
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),
让其在各个页面上实现数据的共享包括状态,并且可操作
Vuex分成五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
3. vuex使用步骤
3.1 安装
npm install vuex -S
3.2 创建store模块,分别维护state/actions/mutations/getters
store
index.js
state.js
actions.js
mutations.js
getters.js
3.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.4 在main.js中导入并使用store实例
import store from './store'
new Vue({
el: '#app',
data(){
return{
/* 容器 */
Bus:new Vue({
})
}
},
router,
store,
components: { App },
template: '<App/>'
})
4. vuex的核心概念:store、state、getters、mutations、actions
4.1store
每一个Vuex应用的核心就是store(仓库),store基本上就是一个容器,它包含着你的应用中大部分的状态 (state)。
const store = new Vuex.Store({
state, // 共同维护的一个状态,state里面可以是很多个全局状态
getters, // 获取数据并渲染
actions, // 数据的异步操作
mutations // 处理数据的唯一途径,state的改变或赋值只能在这里
})
4.2 state(保存数据的容器)状态,即要全局读写的数据
const state = {
resturantName:'飞歌餐馆'
};
4.2 getters(getXxx)获取数据并渲染,
const getters = {
resturantName: (state) => {
return state.resturantName;
}
};
在VuexPage1.vue 中获取值
注1:getters将state中定义的值暴露在this.store.getters对象中,我们可以通过如下代码访问this. store.getters对象中,我们可以通过如下代码访问this.store.getters对象中,我们可以通过如下代码访问this.store.getters.resturantName
注2:state状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态,如下:
computed: {
resturantName: function() {
return this.$store.getters.resturantName;
}
}
<template>
<div>
<ul>
<li>这是第一张页面:{{msg}}</li>
<li>
<input v-model="newName" />
<button @click="buy">盘它</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newName:''
};
},
computed:{
msg(){
//不推荐,因为vuex遵循了编码解耦的原则,各模块各司其职
return this.$store.state.resturantName;
}
},
methods:{
buy(){
console.log(this.newName);
this.$store.commit('setResturantName',{
resturantName:this.newName
})
}
}
}
</script>
<style>
</style>
VuexPage2.vue:
<template>
<div>
<ul>
<li>这是第二张页面:{{msg}}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
};
},
computed:{
msg(){
return this.$store.getters.getResturantName;
}
}
}
</script>
<style>
</style>
添加路由index.js:
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import AppMain from '@/components/AppMain'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'
import Login from '@/views/Login'
import Reg from '@/views/Reg'
Vue.use(Router)
export default new Router({
routes: [{
path: '/',
name: 'Login',
component: Login
},
{
path: '/Login',
name: 'Login',
component: Login
},
{
path: '/Reg',
name: 'Reg',
component: Reg
},
{
path: '/AppMain',
name: 'AppMain',
component: AppMain,
children: [{
path: '/sys/Articles',
name: 'Articles',
component: Articles
},
{
path: '/sys/VuexPage1',
name: 'VuexPage1',
component: VuexPage1
},
{
path: '/sys/VuexPage2',
name: 'VuexPage2',
component: VuexPage2
}
]
}
]
})
4、Vuex的异步加载问题
VuexPage1.vue:
<template>
<div>
<ul>
<li>这是第一张页面:{{msg}}</li>
<li>
<input v-model="newName" />
<button @click="buy">盘它</button>
<button @click="badBuy">有阴谋</button>
<button @click="doAjax">调用ajax</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newName:''
};
},
computed:{
msg(){
//不推荐,因为vuex遵循了编码解耦的原则,各模块各司其职
return this.$store.state.resturantName;
}
},
methods:{
buy(){
console.log(this.newName);
this.$store.commit('setResturantName',{
resturantName:this.newName
})
},
badBuy(){
this.$store.dispatch('setResturantNameAsync',{
resturantName:this.newName
})
},
doAjax(){
this.$store.dispatch('doAjax',{
_this:this
})
}
}
}
</script>
<style>
</style>
actions.js:
export default{
setResturantNameAsync:(context,payload)=>{
console.log('xxx');
setTimeout(()=>{
console.log('yyy');
context.commit('setResturantName',{
resturantName:payload.resturantName
})
},6000);
console.log('zzz');
},
doAjax:(context,payload)=>{
//this.axios 不能用this,获取不到
let _this=payload._this;
let url = _this.axios.urls.SYSTEM_MENU_TREE;
_this.axios.post(url, {}).then((response) => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
}
}