Skip to content

Instantly share code, notes, and snippets.

@shukn
Last active May 28, 2019 17:01
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 shukn/550e542ba292725c624a51bd257c277c to your computer and use it in GitHub Desktop.
Save shukn/550e542ba292725c624a51bd257c277c to your computer and use it in GitHub Desktop.
Vuex調査
<template>
<div>
{{ storeCount }}
{{ localCount }}
{{ totalCount }}
<input type="button" @click="storeCountIncrement" value="increment">
</div>
</template>
<script>
export default {
data: function(){
return {
count: 11,
}
},
computed: {
storeCount () {
return this.$store.getters.storeCount;
},
localCount () {
return `local count: ${this.count}`;
},
totalCount () {
return this.$store.getters.totalCount(this.count);
},
},
methods: {
storeCountIncrement(){
this.$store.dispatch('increment', {num: 1});
}
}
}
</script>
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
count: 1,
},
getters: {
storeCount (state) {
return `store count: ${state.count}`
},
totalCount(state) {
return function(localCount){
return `total count: ${state.count + localCount}`
};
}
},
mutations:{
increment(state, payload){
state.count += payload.num;
}
},
actions: {
increment(context, payload){
context.commit('increment', payload);
}
}
})
new Vue({
store,
render: h => h(App),
}).$mount('#app')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment