localStorage.setItem() is used when the front and back ends are separated

localStorage is used to save the data of the entire website for a long time. The saved data has no expiration time until it is manually deleted.

The localStorage property is read-only.

When the front and back ends are separated, the data returned by the back end can be saved in localStorage:

  localStorage.setItem("user",JSON.stringify(res.data.user));

Then the system jumps to the page to read user information:

  var userString = localStorage.getItem("user");

After clicking log out, localStorage data will be deleted.

 localStorage.removeItem("user");

 The page detects whether there is a user logged in, and does not jump to the login interface:

 if (userString) {
                this.user = JSON.parse(userString);
            } else {
                alert("您尚未登录,点击确定跳转至登录页面!");
                location.href = "/ems_vue/login.html";
            }

 

Guess you like

Origin blog.csdn.net/weixin_38959210/article/details/108514885