The best way is to use VUEX : vue state management system
In the vuex store.js file, you can determine the state of the user as follows:
const store = new Vuex.Store({
state: {
user: {
userName:'',
loggedInStatus: true,
authToken: ''
}
},
mutations: {
addWebToken: function(state, webToken){
state.user.authToken = webToken;
},
removeWebToken: function(state){
state.user.authToken = '';
}
},
actions: {
login: function(context, userInput){
$.ajax({
url: "/vue/api/login",
type: "POST",
async:false,
data:{
username:userInput.username,
password: userInput.password
},
success: function (data) {
context.commit('addWebToken', webToken);
router.push({
path: '/employeeList',
});
},
error:function (xhr, status, error) {
_this.errorMessage = xhr.responseText
}
});
},
logput: function(context){
context.commit('removeWebToken');
}
}
})
> > login() , vuex srore,
var Login = Vue.extend({
template: '#auth',
data: function () {
return {username: '',password: '',errorMessage:''};
},
methods : {
login: function () {
this.$store.dispatch('login', {username: this.username, ppassword: password});
},
logout: function(){
this.$store.dispatch('logout');
}
}
});
:
const store = new Store({
// ...
plugins: [ createPersistedState({
getState: (key) => Cookies.getJSON(key),
setState: (key, state) => Cookies.set(key, state, { expires: 3, secure: true })
}) ]
})
vustx persisted state jscookies, , , vuex-persistedstate