vue传参
事件总线(vue实例) $bus
main文件注册:
new Vue({
el: '#app',
router,
store,
render: h => h(App),
beforeCreate() {
window.$bus = Vue.prototype.$bus = this
}
})
this.$bus.$emit(方法名,传参)
this.$bus.$on(对应的方法名,传过来的数据=>{
接收数据
})
父孙组件通信
a
t
t
r
s
和
attrs和
attrs和listeners
在孙组件上绑定属性
<Son1 v-bind="$attrs" v-on="$listeners" />
孙组件就可通过$attrs对象(没有被props接收的值集合)
拿到父组件传过来的值并且
通过this.$emit(方法名,传参)传参了
vuex
const state = {
num: 0,
person: {
name: "张三",
age: 18,
},
hobby: [],
};
const mutations = {
SET_NUM: (state, num) => {
state.num = num;
},
SET_PERSON: (state, person) => {
state.person = person;
},
SET_HOBBY: (state, hobby) => {
state.hobby = hobby;
},
};
const actions = {
changeNum({ commit }, num) {
commit("SET_NUM", num);
},
editPerson({ commit }, obj) {
commit("SET_PERSON", obj);
},
handleHobby({ commit }, arr) {
commit("SET_HOBBY", arr);
},
triggerHobby({ dispatch }, arr) {
dispatch("handleHobby", arr);
},
};
export default {
namespaced: true,
state,
mutations,
actions,
};
testNum:state => state.test.num,
import { mapState,mapGetters } from "vuex";
computed:{
...mapState(["test"]),
...mapState({
curNum: (state) => state.test.num,
person: (state) => state.test.person,
hobby: (state) => state.test.hobby,
}),
...mapGetters(["testNum"]),
},
this.$store.dispatch('test/setSize', size)