vue.js中$emit的理解


 

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--父組件是使用 props 傳遞數據給子組件,但如果子組件要把數據傳遞回去則使用自定義事件!-->
<div id="app">
<p>{{total}}</p>
<button-counter v-on:increment='incrementTotal'></button-counter>
<button-counter @increment='incrementTotal'></button-counter>
</div>

</body>
<script type="text/javascript" src="vue.js"></script>
<script type="text/javascript">
Vue.component('button-counter',{
template:'<button v-on:click="increment">{{counter}}</button>',
data:function(){
return {
counter:0
}
},
methods:{
increment:function(){
this.counter +=1;
//$emit(‘increment1‘,[12,‘kkk‘]),直接看是懵逼的有沒有,可以先告訴你,就是觸發自定義事件increment1(或者函數名吧),[]為參數
this.$emit('increment')
}
}
})

var app = new Vue({
el:'#app',
data:{
total:0
},
methods:{
incrementTotal:function(){
this.total += 1;
}
}
})
</script>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM