子组件派发事件和值给父组件

API

子组件通过$emit派发事件和值给父组件(值可以有多个)

1
this.$emit('fnX', value)

父组件通过v-on绑定子组件派发的事件,并触发一个新的事件,新的事件内可以接收传来的值

1
2
3
4
5
6
7
8
<ComponentName @fnX="fnY"></ComponentName>


methods: {
fnY(value) {
console.log(value)
}
}

父子组件间传递数据 demo

See the Pen mdJVqgg by xugaoyi (@xugaoyi) on CodePen.

1
&lt;script async src="https://static.codepen.io/assets/embed/ei.js">&lt;/script>

来源:本文导入自 xugaoyi/vuepress-theme-vdoingdocs/《Vue》笔记/02.组件/50.子组件派发事件和值给父组件.md
原作者:xugaoyi。许可证:MIT