<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Vue.js</title>
</head>
<body>
<div id="app">
<p>現在値:{{ current }}</p>
<my-counter step="1" v-on:plus="onplus"></my-counter>
<my-counter step="2" v-on:plus="onplus"></my-counter>
<my-counter step="-1" v-on:plus="onplus"></my-counter>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script src="js/emit_basic.js"></script>
</body>
</html>
Vue.component('my-counter', {
props: [ 'step'],
template: `<button type="button" v-on:click="onclick">
{{ step }}</button>`,
methods: {
onclick() {
this.$emit('plus', Number(this.step));
}
}
});
new Vue({
el: '#app',
data: {
current: 0
},
methods: {
onplus: function(e) {
this.current += e;
}
}
})