Skip to content

Instantly share code, notes, and snippets.

@nasum
Last active December 17, 2017 14:50
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 nasum/1cb0995a9fa97a23a48146b205fdd5c5 to your computer and use it in GitHub Desktop.
Save nasum/1cb0995a9fa97a23a48146b205fdd5c5 to your computer and use it in GitHub Desktop.
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
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')
}
}
})
methods: {
increment() {
this.$store.dispatch('increment')
}
}
methods: {
increment() {
this.$store.dispatch({
type: 'increment',
num: 2
})
}
}
actions: {
increment(context, payload) {
context.commit({
type: 'increment',
num: payload.num
})
}
}
<h1>Counter</h1>
<div id="app">
<div>
{{ showCounter }}
</div>
<button @click="increment">
increment
</button>
</div>
getters: {
countWithSuffix(state) {
return `${state.count} 回`
}
},
mutations: {
increment(state) {
state.count++
}
},
getters: {
countWithSuffix(state, getters) {
return `${getters.countWithPrefix} 回`
},
countWithPrefix(state) {
return `第 ${state.count}`
}
},
actions: {
increment(context) {
context.commit('increment')
}
}
mutations: {
increment(state, num) {
state.count += num
}
},
actions: {
increment(context) {
context.commit('increment', 1)
}
}
mutations: {
increment(state, payload) {
state.count += payload.num
}
},
actions: {
increment(context) {
context.commit({
type: 'increment',
num: 1
})
}
}
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