子页面:
<template>
<div>
<p>子组件</p>
<button @click="sendMsg">传递到父页面</button>
</div>
</template>
<script>
export default {
name: ‘child‘,
data() {
return {
msg:‘子组件数据‘
}
},
computed:{
addNum(){
return this.num*5
}
},
methods:{
sendMsg(event){
this.$emit(‘sendmsg‘,this.msg)
this.$emit(‘addnum‘,this.addNum)
}
},
props:{
num:{
type:Number,
default:5
}
}
}
</script>
<style>
</style>
父页面:
<template>
<div>
<p>父组件</p>
<input type="text" v-model="num" />
<child @sendmsg=‘getMsg‘ :num=‘num‘ @addnum=‘getNum‘/>
<p>{{info}}</p>
<p>{{num}}</p>
</div>
</template>
<script>
import child from ‘./child‘
export default {
name: ‘parent‘,
data() {
return {
info:‘‘,
num:10
}
},
components:{
child
},
methods:{
getMsg(data){
this.info = data
},
getNum(data){
this.num = data
}
}
}
</script>
<style>
</style>
原文地址:https://www.cnblogs.com/qiyc/p/9113897.html