Component is VueJS
one of the most powerful features, by the combination of components, applications like pushing a combination of building blocks. Between the various components are independent, which means that data between different components can not be shared, so that the communication between the components is particularly important, the article describes the communication between the components in detail VueJS
Component Diagram
From the figure we can clearly understand the relationship between the components. We need to choose a different way of communication between the different components for
props/$emit`
props
Attribute is a parent component by way of the communication subassembly parent component byprop
passing, through the sub-assemblyprops
Statement
- Sub-assemblies, components can be passed to the parent value through custom events
$emit
passed way, sub-components distribution events, parent component listens for events
$ Emit / $ on (event bus)
Communication between components by way of a bus event we can achieve a relationship or no relationship, if you encounter more complex projects, it is recommended to use Vuex to solve
We can new Vue()
an empty Vue
instance of the bus as a whole project, distribute and monitor the realization of events
const bus = new Vue()
Vue.prototype.$bus = bus
Throughout the project components, can be used this.$bus.$emit()
to distribute events, use this.$bus.$on()
to listen for events
$attrs/$listeners
$attrs
: It contains the parent scope is notprop
bound to the identified characteristics (style / class excluded). When a component is noprop
time, there will bind all contain the parent scope, and can bev-bind="$attrs"
passed internal components$listeners
: Contains the parent scope of thev-on
event listener, it can bev-on="$listeners"
an internal incoming components
The results of printing
provide/inject
An ancestor component used provide
to inject a dependency to future assembly component used in the offspring inject
to receive variables ancestor component provides.
$parent/$children/$ref
- $ Ref: If used in a generic DOM element reference point is the DOM elements, if used in the subassembly, is a reference to a component instance
$parent
/$children
: Access component instance Sons