Created
July 30, 2018 16:42
-
-
Save afontcu/bd3a8885e6e748d5825152c42ab4b40d to your computer and use it in GitHub Desktop.
This file contains 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
<template> | |
<div> | |
<h1>Election day!</h1> | |
<button @click="voteForRed">Vote for 🔴</button> | |
<button @click="voteForBlue">Vote for 🔵</button> | |
<h2>Results</h2> | |
<results/> | |
<total-votes/> | |
</div> | |
</template> | |
<script> | |
import Vue from 'vue' | |
const createStore = ({ state, mutations }) => { | |
return new Vue({ | |
data () { | |
return { state } | |
}, | |
methods: { | |
commit (mutationName) { | |
mutations[mutationName](this.state) | |
}, | |
}, | |
}) | |
} | |
const store = createStore({ | |
state: { red: 0, blue: 0 }, | |
mutations: { | |
voteForRed (state) { state.red++ }, | |
voteForBlue (state) { state.blue++ }, | |
}, | |
}) | |
const TotalVotes = { | |
render: h => h('div', `Total votes: ${store.state.red + store.state.blue}`), | |
} | |
const Results = { | |
render: h => h('div', `Red: ${store.state.red} - Blue: ${store.state.blue}`), | |
} | |
export default { | |
components: { TotalVotes, Results }, | |
methods: { | |
voteForRed () { store.commit('voteForRed') }, | |
voteForBlue () { store.commit('voteForBlue') }, | |
}, | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment