```
vuex+webpack
回顾
父传子->props
- 子组件props:["a"]
- 使用组件<a :a="父组件的data数据">
- 子组件{{a}}
子传父->this.$emit
- 使用子组件时<a @自定义事件名b="父组件fn方法">
- 子组件中触发事件b this.$emit("b",参数c)
- 父组件中fn(argv){argv就是参数c}
注意:this.$emit()->触发事件
补充: props是单向数据流
兄弟组件B把data的数据传递给兄弟组件C使用
busEvent.js(中央事件总线):给其他文件提供了共享/公用的对象(newVue())
注意:先绑定事件,再触发事件
vuex是Vue插件->状态管理->数据管理->
- 多个组件使用同一个数据
- 父传子
- 子传父
- 兄弟组件
vuex使用场景:只针对数据复杂的Vue项目
vuex流程
非常重要!
作用: 声明数据
使用:
- 视图中
{{$store.state.count}}
- js->state的count对应的组件的计算属性
computed: {
count() {
return this.$store.state.count;
},
目的:简化state的使用
computed:{
...mapState(["count"])
}
// 视图中{{count}}
getters:不是Vuex核心组成部分
场景:如果state中的数据b依赖了数据a,此时b就是getters的数据
getters: {
msgNew(state) {
return state.msg + "xyz"
}
}
使用
<p>{{$store.getters.msgNew}}</p>
computed: {
...mapGetters(["msgNew"]),
...mapState(["count"])
},
mutations:修改state的数据
mutations: {
fn1(state, payload) {
console.log(payload);
console.log("fn1-----");
state.count = payload.num;
}
}
组件的methods
fn11() {
// 调用mutations中的fn1
this.$store.commit("fn1", { num: 200 });
}
mutations中的方法触发的条件->必须通过commit()
mutations中的方法在组件中的接收者时methods
methods: {
// fn11(){
// this.$store.commit("fn1",100)
// }
// 在methods中有一个fn1方法,
getData() {
this.fn2();
},
// fn2(){
// this.$store.commit("fn2")
// }
...mapMutations(["fn2"])
}
注意: mutations方法只能写同步的
应用vuex->小项目->模板/套路+webpack配置(了解)->大厂->webpack配置工程师
```
原文:https://www.cnblogs.com/divtab/p/11312722.html