<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ref</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
<div id="app">
<counter :count="0" @change="handleSum"></counter>
<counter :count="0" @change="handleSum"></counter>
<div>{{sum}}</div>
</div>
<script>
var counter = {
props: ['count'],
data: function() {
return {
number: this.count,
}
},
template: "<div @click='handleNumPlus'>{{number}}</div>",
methods: {
handleNumPlus: function() {
this.number = this.number + 1;
this.$emit('change', 1)
}
}
};
var vm = new Vue({
el: '#app',
data: {
sum: 0
},
methods: {
handleSum: function(step) {
this.sum += step;
}
},
components: {
counter: counter
}
})
</script>
</body>
</html>
网友评论