[Vuejs]-Vuex not change isLogin variable to false ()

0👍

Your mutation changeIsLogin is basically adding the payload to the current state.
So your code:

changeIsLogin (state, payload) {
    state.isLogin += payload
}

Is basically equivalent to:

changeIsLogin (state, payload) {
    state.isLogin = state.isLogin + payload
}

Since boolean values can act like number when doing arithmetic operations on them, you’ll have the following issues:

state.isLogin = false
payload = true
--> 
state.isLogin = true + false = 1

state.isLogin = 1
payload = false
--> 
state.isLogin = 1 + false = 1

and so on.

So all you have to do is remove the arithmetic operation:

changeIsLogin (state, payload) {
    state.isLogin = payload
}

Leave a comment