Who is the parent component and who is the child component in vue?

is the parent component counter-event-example ? Is the subcomponent button-counter ? counter-event-example is just a div tag, isn"t it? where are the components? v-on is listening? If that"s the case, aren"t you eavesdropping on yourself? I can"t tell which is the parent component and which is the child component. What if I can"t figure out the use of emit?

<div id="counter-event-example">
  

{{ total }}

<button-counter v-on:increment1="incrementTotal"></button-counter> <button-counter v-on:increment1="incrementTotal"></button-counter> </div> Vue.component("button-counter", { template: "<button v-on:click="increment">{{ counter }}</button>", data: function () { return { counter: 0 } }, methods: { increment: function () { this.counter += 1 //counter this.$emit("increment1")// } }, }) new Vue({ el: "-sharpcounter-event-example", data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 //total } } })
Mar.28,2021

you see that you have registered a component button-counter , and then this component is called in counter-event-example , so it is a subcomponent of counter-event-example . After
, if you want to call the parent component's counter-event-example method in button-counter , pass in the parent component's method when using the child component button-counter , and then pass the parameter call to the $emit method in the child component.


Yes, the parent component is the counter-event-example, child component and the button-counter, parent child component refers to another component when registering another component.

<component1>
    <component2></component2>
</component1>

component1 component2 is a sibling component, and yours is a parent-child component. The parent component is listening, listening to the child component. Writing this.$emit (), in the event of the child component will trigger the custom event incrementTotal.


parent components can use props to pass data to child components. The
child component can use $emit to trigger a custom event for the parent component.
generally speaking, it should be this.$emit ('xxxx', [data])
xxxx is the custom event name, data is the optional data passed, empty is OK.
the parent component should bind an event through the vmerchant this.$emit xxxxx event, which is triggered when the child component xxxxx is called.
as for who is the child component and who is the parent component, just look at the call, and whoever is called is the child component.


you asked my heart

Menu