Last active
June 15, 2018 01:19
-
-
Save djirdehh/c793d9ff50fc46f3ef9f5b3248be2180 to your computer and use it in GitHub Desktop.
Example of using custom events in Vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Parent Component --> | |
<template> | |
<div> | |
<ChildComponent | |
:numbers="numbers" | |
@number-added="numbers.push($event)" | |
/> | |
</div> | |
</template> | |
<script> | |
import ChildComponent from "./ChildComponent"; | |
export default { | |
name: "ParentComponent", | |
data() { | |
return { | |
numbers: [1, 2, 3] | |
}; | |
}, | |
components: { | |
ChildComponent | |
} | |
}; | |
</script> | |
<!-- Child Component --> | |
<template> | |
<div> | |
<h2>The numbers are {{ numbers }}!</h2> | |
<input v-model="number" type="number" /> | |
<button @click="$emit('number-added', Number(number))"> | |
Add new number | |
</button> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: "ChildComponent", | |
props: { | |
numbers: Array | |
}, | |
data() { | |
return { | |
number: 0 | |
}; | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment