Home > Web Front-end > JS Tutorial > Regarding the implementation of dispatch mechanism between Vue2.0 parent and child components (detailed tutorial)

Regarding the implementation of dispatch mechanism between Vue2.0 parent and child components (detailed tutorial)

亚连
Release: 2018-06-12 18:26:38
Original
1621 people have browsed it

This article mainly introduces the event dispatching mechanism between Vue2.0 parent and child components. Now I will share it with you and give you a reference.

Everyone who has come from vue1.x knows that in vue2.0, $dispatch and $broadcase for event communication between parent and child components have been removed. The official consideration is that the event flow method based on the component tree structure is really difficult to understand, and will become more and more brittle as the component structure expands. Especially when the component hierarchy is relatively deep. Through the mechanism of broadcasting and event distribution, it seems more confusing.

While abolishing it, the official also provided us with a replacement plan, including instantiating an empty vue instance and using $emit to react to state changes on the subcomponent

1. Use $emit to trigger the event

helloWorld.vue as the parent component, and the dialogConfigVisible variable controls the display or hiding of the sub-component pop-up box.

configBox.vue is used as a subcomponent, assuming it is an encapsulated announcement pop-up window.

In helloWorld.vue in the parent component