SubComponentA (子组件)
<template>
<div @click="sendApp">点击我给父组件传值</div>
</template>
<script>
export default {
name: "subComponent",
data() {
return {
value: "这是传给父组件App的值"
};
},
methods: {
sendApp() {
this.$emit("sendValue", this.value);
}
}
};
</script>
父组件 (App.vue)
<template>
<sub-component-a @sendValue="getSubValue" />
</template>
<script>
import SubComponentA from "./components/SubComponentA";
export default {
components: {
SubComponentA
},
methods: {
getSubValue(value) {
alert(value);
}
}
};
</script>
子组件给父组件传值的重点
子组件:
this.$emit(事件名,传递的值)
父组件:
<子组件 :事件名="执行的事件" />