Vue.js Components - Custom Events
Parent components use props to pass data to child components, but if a child component needs to pass data back, custom events must be used!
We can use v-on to bind custom events. Every Vue instance implements the Events interface, namely:
- Use
$on(eventName)Listen for events - Use
$emit(eventName)Trigger events
In addition, the parent component can use v-on directly where the child component is used to listen for events triggered by the child component.
In the following example, the child component is completely decoupled from its outside. All it does is trigger an internal event that the parent component cares about.
Example
<div id="app">
<div id="counter-event-example">
<p>{{ total }}
p
<
button-counter
v-on:increment
=
"
incrementTotal
"
button-counter
<
button-counter
v-on:increment
=
"
incrementTotal
"
button-counter
div
div
<
scriptVue.component('button-counter', { template: '
<button v-on:click="incrementHandler">{{ counter }}
button
', data: function () { return { counter: 0 } }, methods: { incrementHandler: function () { this.counter += 1 this.$emit('increment') } }, }) new Vue({ el: '#counter-event-example', data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 } } })
script
Try it »
If you want to listen to a native event on the root element of a component, you can use .native to modify v-on. For example:
data must be a function
In the above example, you can see that the data in the button-counter component is not an object but a function:
data: function () {
return {
count: 0
}
}
The benefit of this is that each instance can maintain an independent copy of the returned object. If data were an object, it would affect other instances, as shown below:
Example
<
div
id
=
"
components-demo3
"
class
=
"
demo
"
<
button-counter2
button-counter2
<
button-counter2
button-counter2
<
button-counter2
button-counter2
div
<
scriptvar buttonCounter2Data = { count: 0 } Vue.component('button-counter2', { /* data: function () { // the data option is a function, components do not affect each other return { count: 0 } }, */ data: function () { // the data option is an object, it will affect other instances return buttonCounter2Data }, template: '
<button v-on:click="count++">Clicked {{ count }} times.
button
' }) new Vue({ el: '#components-demo3' })
script
Try it »
v-model on custom components
v-model on a component by default uses a prop named value and an event named input.
Equivalent to:
In the following example, the custom component runoob-input is defined, the parent component's num has an initial value of 100, and changing the child component's value updates the parent's num in real time:
Example
<
div
id
=
"
app
"
<
runoob-input
v-model
=
"
num
"
runoob-input
<
pThe entered number is: {{num}}
p
div
<
script
Vue.component('runoob-input', { template: `
<p>
<input ref="input" :value="value" @input=">$emit('input', $event.target.value)
"
p
`, props: ['value'], // the prop named value }) new Vue({ el: '#app', data: { num: 100, } })
script
Try it »
Since v-model passes value by default, not checked, for checkbox or radio button components we need to use the model option, which can specify the current event type and the passed props.
Example
<
div
id
=
"
app
"
<
base-checkbox
v-model
=
"
lovingVue
"
base-checkbox
<
div
v-show
=
"
lovingVue
"I will be displayed if the checkbox is checked.
div
div
<
script
// register Vue.component('base-checkbox', { model: { prop: 'checked', event: 'change' // onchange event }, props: { checked: Boolean }, template: `
<input type="checkbox" v-bind:checked="checked" v-on:change=">$emit('change', $event.target.checked)
"
` }) // create root instance new Vue({ el: '#app', data: { lovingVue: true } })
script
Try it »
In the example, the value of lovingVue is passed to the checked prop, and at the same time when