定义
$emit
的主要作用是子组件触发父组件的事件。
语法:
$emit(event, args)
$emit
主要用于子组件中某些的事件需要父组件做出响应时的场景。
示例
<template>
<div>
<input v-model="name" @input="handleChange">
</div>
</template>
<script>
export default {
name: "Child",
data(){
return {
name:'Jack'
}
},
methods: {
handleChange(){
this.$emit('change', this.name)
}
}
}
</script>
<template>
<div>
<h1> hello {{name}}</h1>
<child @change="parentEvent"></child>
</div>
</template>
<script>
import Child from "@/components/Child";
export default {
name: "Parent",
components: {Child},
data(){
return {
name:'Tom'
}
},
methods: {
parentEvent(name) {
this.name = name;
}
}
}
</script>

