vue中各个组件之间传值比较
1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);
缺点:用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
2.非父子组件或父子组件
通过数据总数Bus,this.root. root.root.emit(‘事件名’,参数1,参数2,…)
缺点: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据
3.非父子组件或父子组件
更好的方式是在vue中使用vuex,实现了数据统计管理。
方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据
Vuex中的各个js文件的用途
变量传值的演变形式
Vuex的使用
(图解Vuex各组件)
在这一张图我们可以看的出Vuex的性能非常好,总线传值有很大的缺陷存在,所以我们这次重点会讲解Vuex这个版块。
1 安装
npm install vuex -S
2 创建store模块,分别维护State/Actions/Mutations/Getters
3 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块
const store = new Vuex.Store({state,getters,actions,mutations})
index.js
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实例
// 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'
// process.env.MOCK && require('@/mock')
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import axios from '@/api/http'
import VueAxios from 'vue-axios'
import store from './store'
Vue.use(VueAxios, axios)
Vue.use(ElementUI)
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
data(){
return{
Bus:new Vue({
})
}
},
router,
store,
components: {
App
},
template: '<App/>'
})
State(保存数据的容器)
状态,即要全局读写的数据
export default{
resturantName:'飞歌餐馆'
}
Getters(getXxx)
store/Getters.js
export default{
resturantName: (state) => {
return state.resturantName;
}
}
State状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态,如下:
return this.$store.state.resturantName;
Mutations(setXxx)
处理数据的唯一途径,state的改变或赋值只能在这里
export default{
setResturantName: (state, payload) => {
state.resturantName = payload.resturantName;
}
}
【注1】Mutations中方法的调用方式
不能直接调用this.$store.Mutations.setResturantName(‘KFC’),必须使用如下方式调用:
this.$store.commit('setResturantName', {
resturantName: '魅力四射养生馆,全套不打折'
});
【注2】一定要记住,Mutation 必须是同步函数。为什么呢?异步方法,我们不知道什么时候状态会发生改变,所以也就无法追踪了
如果我们需要异步操作,Mutations就不能满足我们需求了,这时候我们就需要Actions了
actions 数据的异步(async)操作
store/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');
//在这个里面是获取怒道vue的跟实列的
},
doAjax: (context, payload) => {
let _this=payload._this
let url = _this.axios.urls.SYSTEM_USER_DOLOGIN;
// let url='http://localhost:8080/t216_SSH/vue/userAction_login.action';
_this.axios.post(url, {}).then((response) => {
console.log(response);
}).catch(function(error) {
console.log(error);
});
},
}
Action类似于 mutation,不同在于:
1.Action提交的是mutation,而不是直接变更状态
2.Action可以包含任意异步操作
3.Action的回调函数接收一个 context 上下文参数,注意,这个参数可不一般,它与 store 实例有着相同的方法和属性
但是他们并不是同一个实例,context 包含:
- state、2. rootState、3. getters、4. mutations、5. actions 五个属性
所以在这里可以使用 context.commit 来提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。
【注1】actions中方法的调用方式语法如下:
buyasync() {
this.$store.dispatch('setResturantNameByAsync', {
resturantName: '精油护体,港派按压'
});
},
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: '魅力四射养生馆,全套不打折'
});
},
buyasync() {
this.$store.dispatch('setResturantNameByAsync', {
resturantName: '精油护体,港派按压'
});
},
doAjax(){
this.$store.dispatch('doAjax', {
_this:this
});
}
},
}
</script>
<style>
</style>
VuexPage2.vue
<template>
<div>
<h3>页面2:欢迎来到{{msg}}</h3>
</div>
</template>
<script>
export default {
data() {
return {
};
},
computed: {
msg() {
return this.$store.state.resturantName;
}
},
}
</script>
<style>
</style>
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.$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异步初始购物车数据