1、 $dispatch
Vue.prototype.$dispatch=function (eventName,componentName,value){
let parent=this.$parent;
while(parent){
if(parent.$options.name===componentName){
parent.$emit(eventName,value);
return
}
parent=parent.$parent;
}
}
1、 $broadcast
// 向下通知,某个组件进行触发事件
Vue.prototype.$broadcast=function (eventName,componentName,value){
// 需要找到儿子组件进行触发
let children=this.$children;
console.log(children);
function broadcast(children){
for(let i=0;i<children.length;i++){
let child=children[i];
if(componentName===child.$options.name){ //找到了当前的组件
child.$emit(eventName,value);
return;
}else{
if(child.$children){
broadcast(child.$children);
}
}
}
}
broadcast(children);
}