vue之prop,emit数据传递示例
parent.vue
<template>
<div>
父亲: {{childInfo}}
<child :parentInfo="parentMsg" @childInfo="handleChildMsg"/>
</div>
</template>
<script>
import child from "./ccc"
export default {
name: "parent",
components: {
child
},
data() {
return {
parentMsg: "来自父亲的话",
childInfo: ""
}
},
methods: {
handleChildMsg(data) {
this.childInfo = data;
}
},
}
</script>
<style scoped>
</style>
child.vue
<template>
<div>
儿子:{{parentInfo}}
<button @click="sendMsg">给父亲发送消息</button>
</div>
</template>
<script>
export default {
name: "child",
data() {
return {
childInfo: "儿子发来的消息"
}
},
props: {//子接收数据
parentInfo: {
type: String,
default: ""
}
},
methods: {
sendMsg() {
this.$emit("childInfo", this.childInfo);
}
},
}
</script>
<style scoped>
</style>