Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<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
You can’t perform that action at this time.