Created
September 16, 2018 08:29
-
-
Save uno-de-piera/8cf4a3ee0b0a2c06710f4bc584d273ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class="cmp" v-if="id"> | |
{{ id }} | |
</div> | |
</template> | |
<script> | |
import {createNamespacedHelpers} from 'vuex'; | |
const { mapActions, mapState } = createNamespacedHelpers( | |
"users" | |
); | |
export default { | |
name: 'cmp', | |
mounted () { | |
this.getUserById(1).then(() => { | |
console.log(this.id); | |
}) | |
}, | |
methods: { | |
...mapActions(['getUserById']) | |
}, | |
computed: { | |
...mapState(["id"]), | |
}, | |
} | |
</script> |
This file contains hidden or 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 userModule from './modules/userModule'; | |
Vue.use(Vuex) | |
export default new Vuex.Store({ | |
modules: { | |
users: userModule | |
} | |
}) |
This file contains hidden or 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
export default { | |
namespaced: true, | |
state: { | |
id: null | |
}, | |
actions: { | |
getUserById ({commit}, userId) { | |
commit('setUserId', userId); | |
} | |
}, | |
mutations: { | |
setUserId (state, id) { | |
state.id = id; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment