自定义事件:
on监听事件和emit触发事件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="vue1.0.25.js" ></script>
</head>
<body>
<div id="app">
<my-btn @total='allcounter()'></my-btn>
<my-btn @total='allcounter()'></my-btn>
<my-btn @total='allcounter()'></my-btn>
<my-btn @total='allcounter()'></my-btn>
<my-btn @total='allcounter()'></my-btn>
<my-btn @total='allcounter()'></my-btn>
<p>所有按钮一共点击了{{totalcount}}次</p>
</div>
<template id="my_btn">
<button @click="total()"> 点击了{{counter}}次</button>
</template>
<script type="text/javascript">
Vue.component(
'my-btn',{
template:'#my_btn',
data(){
return {counter:0}
},
methods:{
total(){
this.counter+=1;
//告诉外界调用了这个方法
this.$emit('total')
}
}
}
)
new Vue({
el:'#app',
data:{
totalcount:0
},
methods:{
allcounter(){
this.totalcount+=1
}
}
})
</script>
</body>
</html>