Vuex
1 . vue中各个组件之间传值
1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);
2.非父子组件或父子组件
通过数据总数Bus,this.root.root.root.emit(‘事件名’,参数1,参数2,…)
3.非父子组件或父子组件
更好的方式是在vue中使用vuex
方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据
2 . Vuex是什么
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),
让其在各个页面上实现数据的共享包括状态,并且可操作
Vuex
Vuex的五个部分:
1.State:单一状态树
2.Getters:状态获取
3.Mutations:触发同步事件
4.Actions:提交mutation,可以包含异步操作
5.Module:将vuex进行分模块
首先在命令窗口输入以下代码
npm install vuex -S
创建store模块,分别维护state/actions/mutations/getters
index.js
state.js
actions.js
mutations.js
getters.js
在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
在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'
// process.env.MOCK && require('@/mock')
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
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/>'
})
Vuex取值
state.js:
export default{
resturantName:'飞歌餐馆'
}
VuexPage1.vue:
<template>
<div>
<ul>
<li>这个是第一张页面:{{msg}}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
};
},
computed:{
msg(){
//不推荐,Vuex遵循了解耦的原则,各模块各司其职
return this.$store.state.resturantName;
}
}
}
</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>
Vuex存值
mutation.js:
export default{
setResturantName: (state, payload) => {
state.resturantName = payload.resturantName;
}
}
VuePage1.vue:
<template>
<div>
<ul>
<li>这个是第一张页面:{{msg}}</li>
<li>
<input v-model="newName" />
<button @click="buy">盘他</button>
<button @click="badBuy">有阴谋</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newName:''
};
},
computed:{
msg(){
//不推荐,Vuex遵循了解耦的原则,各模块各司其职
return this.$store.getters.getResturantName;
}
},
methods:{
buy(){
this.$store.commit('setResturantName',{
resturantName:this.newName
})
}
}
}
}
</script>
<style>
</style>
同步和异步
actions.js:
export default {
setResturantNameAsync: (content, payload) => {
console.log('xxx');
setTimeout(() => {
console.log('yyy');
content.commit('setResturantName', {
resturantName: payload.resturantName
})
}, 6000);
console.log('zzz');
}
}
VuexPage1.vue:
<template>
<div>
<ul>
<li>这个是第一张页面:{{msg}}</li>
<li>
<input v-model="newName" />
<button @click="buy">盘他</button>
<button @click="badBuy">有阴谋</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newName:''
};
},
computed:{
msg(){
//不推荐,Vuex遵循了解耦的原则,各模块各司其职
return this.$store.getters.getResturantName;
}
},
methods:{
buy(){
this.$store.commit('setResturantName',{
resturantName:this.newName
})
},
badBuy(){
this.$store.dispatch('setResturantNameAsync',{
resturantName:this.newName
})
}
}
}
</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)=>{
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);
});
}
}