<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>子给父传值</title>
</head>
<body>
<div class="itany">
<father></father>
</div>
<script src="./vue.js"></script>
<script>
Vue.component('father',{
template:<div> <h1>{{num2}}</h1> <child @meth='the'></child> </div>
,
data:function(){
return{
num2:''
}
},
methods:{
the:function(txt){
this.num2=txt
}
}
})
Vue.component('child',{
template:<button @click="show">传给父元素</button>
,
data:function(){
return{
num:'我是子组件,我要传值给父组件'
}
},
methods:{
show:function(){
this.$emit('meth',this.num)
}
}
})
new Vue({
el:'.itany'
})
</script>
</body>
</html>
网友评论