Communication between components (sub parent mass)

vue neutron transfer data to the parent component is generally used $ emit custom events, listen for this event in the parent assembly and associated logic written in callbacks.

// parent component subassembly defined event listener
  < Editor : inputIndex = "index" @editorEmit = 'editorEmit' > </ Editor >
// return subassembly need to perform the data, and data can be transferred
this.$emit('editorEmit', data)

So the question is, do I really need to return to the whereabouts of the parent component of this data can be executed when the sub-assembly or want to pass data to the data transmission sub-assembly changes need to pass a custom event with, then another a scene data but sub-assemblies, sub-assemblies require the parent component does not know or can not afford the time you want the parent component to parent component, so this time we should use an option ref components:

<editor ref="editor" @editorEmit='editorEmit'></editor>

 

Parent component is defined in the tag ref attribute, call this directly in the js. $ Refs.editor is to call the entire sub-assembly, all the contents of sub-assemblies to call by ref can, of course, is not recommended because it makes the data look very confusing,

So we can define a parent component designed for function calls, such as our child is returned data in a data in this function in the sub-assembly, assembly when a parent wants to get the data directly take the initiative to call ref perform this function get this data, so can adapt to a large part of the scene, logic is a little clearer.

In addition, the father of the child data transfer can also be used ref, there are times it requires a lot of calls to the same sub-assembly in a parent components, and each pass called prop data are different, and pass operation after the data as it changes, so I need to It defines a number of relevant data in the data and change it, I can call directly ref function subassembly direct the data to the sub-assembly in the form of parameters, once the logic clear.

If you call the base component can invoke various functional interfaces ref perform basic components exposed in the parent component, such as display, disappeared.

Guess you like

Origin www.cnblogs.com/happyeveryuday/p/11840092.html