weekCodeing / interview-answe

🌍 前端程序员训练 求星星 ✨ 各位同学可以在issues中提问,无论是实际项目中遇到的问题,或者是技术问题都可以, 大家一起解决💯 👍 😄。
http://www.dadaqianduan.cn/
76 stars 9 forks source link

203.[vue]Vue 组件如何通信? #203

Open webVueBlog opened 4 years ago

webVueBlog commented 4 years ago

[vue]

webVueBlog commented 4 years ago

父子组件:父 => 子 使用 props ,子 => 父 使用 $on 与 $emit (简易的发布订阅模式)

获取父子组件实例:$prarent 、$children (组件初始化时获取)

在父组件中提供数据子组件消费:Provide 与 Inject

1.Provide :将provide数据写到当前组件实例上 2.Inject:遍历父组件查找 provide 数据(响应式数据) 3.多用于写插件

Ref 获取组件实例,调用属性与方法

event bus 事件总线实现跨组件通信 ,Vue.prototype.$bus = new Vue( )

Vuex 状态管理实现