VUE.JS(3)
程序员文章站
2022-06-06 19:57:59
...
父子组件传值
<template>
<div>
<h1>父组件</h1>
<son :toSonData='toSonData' @toFatherData="sendSonData"></son>
<p>parentGet-{{ msg }}</p>
</div>
</template>
<script>
import son from './son.vue'
export default {
data () {
return {
toSonData:'parentGift',
msg:''
};
},
components:{
son
},
methods:{
sendSonData(data){
this.msg=data;
}
}
}
</script>
<style scoped>
</style>
<template>
<div>
<h3>子组件</h3>
<p>获取的礼物-{{ toSonData }}</p>
<button @click="toFatherData">给爸爸传值</button>
</div>
</template>
<script>
export default {
data () {
return {
msg:'sonHello'
};
},
props:['toSonData'],
methods:{
toFatherData(){
this.$emit("toFatherData","sonHello")
}
}
}
</script>
<style scoped>
</style>