子組件向父組件傳值,主要是通過this.$emit('函數(shù)名fun',所傳數(shù)據(jù)datas)將值在子組件標簽綁定事件 @函數(shù)名fun='父組件中的方法father',然后在父組件中定義方法?
father(val){
val就是要傳的數(shù)據(jù)datas
},
這樣就可將之傳遞到父組件中可以進行使用
<body>
<div id="app">
<list-btn @demo='getDatas'></list-btn>
<p>{{msg}}</p>
</div>
</body>
<script type="text/javascript">
Vue.component('list-btn',{
data:function(){
return {
count:0
}
},
template:`
<div>
<button @click='hander'>點擊</button>
</div>
`,
methods:{
hander:function(){
this.$emit('demo',this.count);
}
}
})
let app=new Vue({
el:'#app',
data:{
msg:'我是father'
},
methods:{
getDatas:function(val){
this.msg+=val;
}
}
})
</script>