Last active
May 28, 2019 17:01
-
-
Save shukn/550e542ba292725c624a51bd257c277c to your computer and use it in GitHub Desktop.
Vuex調査
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> | |
{{ 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> |
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
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