Created
December 22, 2016 05:35
-
-
Save acoshift/b65e8bba9110119a498ab6de72de38bf to your computer and use it in GitHub Desktop.
Vuejs Global State + Mixin
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
<!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