[英]Vue JS access objects of "parent component's sibling component"
使用Vuex等状态管理进行非父子组件之间的通信。
将事件发送到$root
或事件总线是不可靠的(容易出现竞争条件)并且难以调试 - 事件捕获将无声地“失败”。 即您永远不会被告知尚未捕获事件。
1) 您必须为每个组件添加一个 ref 标签。 <app ref="App">
, <index ref="Index">
, <incidents ref="Incidents">
, <incident-by-prio ref="IncidentByPrio">
并像这样从根 A 组件调用它:
this.$root.$refs.App.$refs.Index.$refs.Incidents.$refs.IncidentsByPrio.SomeMethod()
编辑1:
2) 从根上的组件 A 发出一个事件。
组分 A
this.$root.$emit('SomeEvent', {...});
在目标组件中捕获此事件:
this.$root.$on('SomeEvent', () => {...});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.