I don't understand how getters work in Vuex. The problem is when logging out the token in state and localStorage is becoming empty, but not in getters.
In created I call this:
created: async function () {
if (this.$store.getters.shortToken) {
this.isConfirm = true
}
console.log(
'signining', localStorage.getItem('token'), // ''
'state', this.$store.state.user.token, // ''
'getter', this.$store.getters.token // 'old-token'
)
if (this.$store.getters.token) {
await this.$router.push({ path: '/' })
}
}
And getters have:
token: state => {
return localStorage.getItem('token') || state.user.token
}
And mutation:
SET_TOKEN: (state, payload) => {
localStorage.setItem('token', payload.token)
Object.assign(state, payload)
}
BUT, console log in created gives me empty localStorage token (which is normal) and empty state.token (which is also normal).. But, getters.token gives me token (which is NOT normal), because I gave SET_TOKEN empty token. Why is this happening?
PS. If I'm adding console.log(state.user.token, localStorage.getItem('token')) above return in getters token, so getters.token in created hook is empty... WHY?
Also some using codes in this situation, this is logout method:
methods: {
async logout () {
if (await this.$store.dispatch('logOut')) {
console.log('logged out')
await this.$router.push({ path: '/signin' })
}
}
}
action logout
async logOut (context) {
console.log('logggog')
context.commit('SET_TOKEN', {
token: ''
})
context.commit('SET_USER', {
user: null
})
return true
}