Skip to content

Instantly share code, notes, and snippets.

@uno-de-piera
Created September 16, 2018 08:29
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 uno-de-piera/8cf4a3ee0b0a2c06710f4bc584d273ad to your computer and use it in GitHub Desktop.
Save uno-de-piera/8cf4a3ee0b0a2c06710f4bc584d273ad to your computer and use it in GitHub Desktop.
<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>
import Vue from 'vue'
import Vuex from 'vuex'
import userModule from './modules/userModule';
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
users: userModule
}
})
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