Last active
December 17, 2017 14:50
-
-
Save nasum/1cb0995a9fa97a23a48146b205fdd5c5 to your computer and use it in GitHub Desktop.
改めて学び直すVuex ref: https://qiita.com/nasum/items/d17c0a628e6c32616b85
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' | |
Vue.use(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
import Vue from 'vue' | |
import Vuex from 'vuex' | |
Vue.use(Vuex) | |
const store = new Vuex.Store({ | |
state: { | |
count: 0 | |
}, | |
getters: { | |
countWithSuffix(state) { | |
return `${state.count} 回` | |
} | |
}, | |
mutations: { | |
increment(state) { | |
state.count++ | |
} | |
}, | |
actions: { | |
increment(context) { | |
context.commit('increment') | |
} | |
} | |
}) | |
const app = new Vue({ | |
el: '#app', | |
computed: { | |
showCounter() { | |
return this.$store.getters.countWithSuffix | |
} | |
}, | |
store, | |
methods: { | |
increment() { | |
this.$store.dispatch('increment') | |
} | |
} | |
}) |
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
methods: { | |
increment() { | |
this.$store.dispatch('increment') | |
} | |
} |
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
methods: { | |
increment() { | |
this.$store.dispatch({ | |
type: 'increment', | |
num: 2 | |
}) | |
} | |
} |
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
actions: { | |
increment(context, payload) { | |
context.commit({ | |
type: 'increment', | |
num: payload.num | |
}) | |
} | |
} |
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
<h1>Counter</h1> | |
<div id="app"> | |
<div> | |
{{ showCounter }} | |
</div> | |
<button @click="increment"> | |
increment | |
</button> | |
</div> |
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
getters: { | |
countWithSuffix(state) { | |
return `${state.count} 回` | |
} | |
}, |
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
mutations: { | |
increment(state) { | |
state.count++ | |
} | |
}, |
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
getters: { | |
countWithSuffix(state, getters) { | |
return `${getters.countWithPrefix} 回` | |
}, | |
countWithPrefix(state) { | |
return `第 ${state.count}` | |
} | |
}, |
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
actions: { | |
increment(context) { | |
context.commit('increment') | |
} | |
} |
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
mutations: { | |
increment(state, num) { | |
state.count += num | |
} | |
}, | |
actions: { | |
increment(context) { | |
context.commit('increment', 1) | |
} | |
} |
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
mutations: { | |
increment(state, payload) { | |
state.count += payload.num | |
} | |
}, | |
actions: { | |
increment(context) { | |
context.commit({ | |
type: 'increment', | |
num: 1 | |
}) | |
} | |
} |
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
actions: { | |
increment(context) { | |
context.commit({ | |
type: 'increment', | |
num: 1 | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment