Created
September 30, 2018 00:58
-
-
Save takaheraw/dfca05eeb2c1d57af939480e1541f15d to your computer and use it in GitHub Desktop.
vue component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script> | |
<div id="fruits-counter"> | |
<div v-for="fruit in fruits"> | |
{{ fruit.name }}: <counter-button v-on:increment="incrementCartStatus()"></counter-button> | |
</div> | |
<p>合計: {{ total }}</p> | |
</div> | |
<script> | |
var counterButton = Vue.extend({ | |
template: '<span>{{ counter }}個<button v-on:click="addToCart">追加</button></span>', | |
data: function() { | |
return { | |
counter: 0 | |
} | |
}, | |
methods: { | |
addToCart: function() { | |
this.counter += 1 | |
this.$emit('increment') | |
} | |
} | |
}) | |
new Vue({ | |
el: '#fruits-counter', | |
components: { | |
'counter-button': counterButton | |
}, | |
data: { | |
total: 0, | |
fruits: [ | |
{ name: '梨' }, | |
{ name: 'イチゴ' }, | |
] | |
}, | |
methods: { | |
incrementCartStatus: function() { | |
this.total += 1 | |
} | |
} | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment