Last active
February 1, 2018 10:07
-
-
Save lsbyerley/1db1aa85b2ff76da2fbcc59cbd4cad0d to your computer and use it in GitHub Desktop.
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
// store/index.js | |
const createStore = () => { | |
return new Vuex.Store({ | |
state: { | |
counter: 0, | |
repos: [] | |
}, | |
actions: { | |
LOAD_REPOS: function({ commit }) { | |
axios.get('url').then((res) => { | |
commit('SET_REPOS', res.data) | |
}) | |
} | |
}, | |
mutations: { | |
SET_REPOS: (state, repos) => { | |
state.repos = repos | |
}, | |
increment(state) { | |
state.counter++ | |
} | |
} | |
}) | |
} | |
// pages/index.vue | |
<script> | |
import { mapState } from 'vuex' | |
export default { | |
fetch ({ store }) { | |
store.dispatch('LOAD_REPOS') | |
}, | |
computed: mapState([ | |
'counter', | |
'repos' | |
]), | |
methods: { | |
increment () { | |
this.$store.commit('increment') | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment