1 父组件
//以下是兄弟组件
<HelloWord name="zujian1" foo="foo" />
2 HelloWord.vue(子组件)
mounted(){
this.$on('foo',()=>{//监听
console.log('foo!!!!!!1')// 输出两次,因为定义了两次相同组件
})
this.$emit('foo')//派发
}
用法:
brother1:
this.$parent.$on('foo',handle)
brother2
this.$parent.$emit('foo')
第二:祖先和后代之间传值
由于嵌套过深,传递props不切合实际,vue提供了provide、inject
1 provide
export default{
provide(){
return {foo:'foo'}
}
}
2 inject(隔代获取参数)
export default{
inject:['foo']
}