DWQA QuestionsCategory: ProgramHow does Vue save the user name?
Codermin asked 2 months ago

A background management system, the user login page top right corner display “welcome, admin”
Admin is the login account
When the login is successful, I will save the token in the cookie. Part of the code is as follows

setCookie('token', this.token , 3600 * 12); // one day
this.$ router.push ('/ home') // login successfully, jump to the home page

But where should I save my user name? If it exists in vuex, it will not be refreshed
Is there a cookie that can store both user name and token?
How do you solve this situation?

5 Answers
Allan91 answered 2 months ago

Refresh and retrieve

dothin answered 2 months ago

If you want to refresh the browser without losing data, you can use cookie, localstorage, sessionstorage and refresh to get it again

Codermin replied 2 months ago

Can cookie store user information and token at the same time

_AN_ answered 2 months ago

The user name and token can be saved in the cookie, because the browser will bring the data in the cookie to the server every time it requests. Therefore, generally, only the interactive data between the front end and the back end is saved in the cookie, and it is recommended to save it in session storage or local storage. Take a look at my articlehttps://segmentfault.com/a/11…

Rossy1 answered 2 months ago
const store = new Vuex.Store({
  //Define state
  state: {
    username: JSON.parse(sessionStorage.getItem('username')) || ""
  mutations: {
    userName(state, msg){
      sessionStorage.setItem('username', JSON.stringify(msg))
      state.username= msg;
dailyvuejs answered 2 months ago

Anyway, you have to have back-end interface support
You should beSingle page applicationbar
First of all, after successful login, you usually request the back-end login interface, such as login. After successful login, you return a token and so on. Then you save the cookie and the interface will carry it every time. The back-end interface needs to be verified