Vuex
传值
一、vue中各个组件之间传值
a、.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.
e
m
i
t
(
′
事
件
名
′
,
参
数
1
,
参
数
2
,
.
.
.
)
;
二
、
非
父
子
组
件
或
父
子
组
件
通
过
数
据
总
数
B
u
s
,
t
h
i
s
.
emit('事件名',参数1,参数2,...); 二、非父子组件或父子组件 通过数据总数Bus,this.
emit(′事件名′,参数1,参数2,...);二、非父子组件或父子组件通过数据总数Bus,this.root.$emit(‘事件名’,参数1,参数2,…)
三、非父子组件或父子组件
更好的方式是在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进行分模块
首先在命令窗口输入以下代码
npm install vuex -S
然后创建store模块,分别维护state/actions/mutations/getters
store
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 */
//将一个空的Vue实例放到根组件下,所有的子组件都能调用
new Vue({
el: '#app',
自定义的事件总线对象,用于父子组件的通信
data(){
return {
Bus:new Vue({
})
}
},
router,
store,
components: { App },
template: '<App/>'
})
state.js 保存数据的容器,保存全局读写的数据
export default {
resturantName: '老王餐馆'
}
getters.js获取数据并渲染
export default {
getResturantName: (state) => {
return state.resturantName;
}
}
mutations.js 处理数据的唯一途径,state的改变或赋值只能在这里
export default {
// type(事件类型): 其值为setResturantName
// payload:官方给它还取了一个高大上的名字:载荷,其实就是一个保存要传递参数的容器
setResturantName: (state, payload) => {
state.resturantName = payload.resturantName;
}
}
actions.js 数据的异步(async)操作
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);
});
}
}
VuexPage1.vue
<template>
<div>
<h3>第一个Vuex页面;{{title}}</h3>
<button @click="changeTitle">餐馆易主</button>
<button @click="changeTitleAsync">两个月后餐馆易主餐馆易主</button>
<button @click="doAjax">测试Vuex中使用ajax</button>
</div>
</template>
<script>
export default {
data() {
return {
// title: ''
};
},
methods: {
changeTitle() {
this.$store.commit('setResturantName', {
resturantName: '老王牛肉餐馆'
})
},
changeTitleAsync() {
this.$store.dispatch('setResturantNameAsync', {
resturantName: '老王羊肉餐馆'
})
},
doAjax(){
this.$store.dispatch('doAjax', {
_this:this
})
}
}
// ,
// created() {
// return this.$store.getters.getResturantName;
// }
/* 这种方法它会双向绑定,一改变都两边的值都会改变 */
computed: {
title() {
// return this.$store.state.resturantName;
return this.$store.getters.getResturantName;
}
}
}
</script>
<style>
</style>
VuexPage2.vue
<template>
<div>
<h3>第二个Vuex页面;{{title}}</h3>
</div>
</template>
<script>
export default {
data() {
return {
title: ''
};
},
created() {
this.title = this.$store.state.resturantName;
}
}
</script>
<style>
</style>