Skip to content

Instantly share code, notes, and snippets.

@acoshift
Created December 22, 2016 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acoshift/b65e8bba9110119a498ab6de72de38bf to your computer and use it in GitHub Desktop.
Save acoshift/b65e8bba9110119a498ab6de72de38bf to your computer and use it in GitHub Desktop.
Vuejs Global State + Mixin
<!doctype>
<html>
<div id="app"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.6/vue.js"></script>
<script>
// create single state instance
const state = {
value: 0
}
Vue.mixin({
// return same instance
data: () => state
})
Vue.component('incr', {
template: `<button @click="action">Incr</button>`,
methods: {
action () {
this.value++
}
}
})
Vue.component('decr', {
template: `<button @click="action">Decr</button>`,
methods: {
action () {
this.value--
}
}
})
new Vue({
el: '#app',
template: `
<div>
Current Value: {{ value }}<br>
<incr/>
<decr/>
</div>
`
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment