@jasen
In order to get the token of the current user when logged in Vuex, you first need to ensure that the token is stored in the Vuex store when the user logs in.
Here is an example of how you can achieve this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// store/auth.js const state = { token: '' }; const mutations = { setToken(state, token) { state.token = token; } }; const actions = { login({ commit }, token) { commit('setToken', token); } }; const getters = { token: state => state.token }; export default { state, mutations, actions, getters }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// MyComponent.vue import { mapGetters } from 'vuex'; export default { computed: { ...mapGetters(['token']), currentToken() { return this.token; } }, created() { console.log('Current user token:', this.currentToken); } }; |
By following these steps, you can store and retrieve the token of the current user when logged in Vuex.