在事件中有html
普通事件:@click/@input/@change/@xxx...事件;vue
修飾符事件:@input.trim,@click.stop,@submit.prevent...通常用於原生html元素;bash
答:在vue中this.$emit(); 返回值是this;函數
代碼示例:ui
在子組件中:Event.vue:接收經過父組件傳遞過來的props:{name:String} 屬性; 在input 標籤中:this
在script 標籤中:spa
<template>
<div>
name: {{ name || "--" }}
<br />
<input :value="name" @change="handleChange" />
<br />
<br />
<div @click="handleDivClick">
<button @click="handleClick">重置成功</button>
<button @click.stop="handleClick">重置失敗</button>
</div>
</div>
</template>
<script>
export default {
name: "EventDemo",
props: {
name: String
},
methods: {
handleChange(e) {
const res = this.$emit("Echange", e.target.value, val => {
console.log(val);
});
console.log(res, res === this);
},
handleDivClick() {
this.$emit("change", "");
},
handleClick(e) {
// 都會失敗
e.stopPropagation();
}
}
};
</script>
複製代碼
在父組件中經過監聽子組件事件,對回調作出響應;code
<template>
<Event :name="name" @Echange="handleEventChange" />
</template>
<script>
import Event from "./Event";
export default {
components: {
Event,
},
data: () => {
return {
name: "",
};
},
mounted() {
},
methods: {
handleEventChange(val, callback) {
this.name = val;
callback("hello");
return "hello";
}
}
};
</script>
複製代碼