In vue, the data is stored in localstroage, and now the data stored in localstroage is used. How to take it out

now I"m going to use the data stored in localstroage. The format in localstroage is a json,. I"m going to use the name, stored in localstroage now. How can I get it? Wait online


has nothing to do with vue.
what you need is Baidu to search for "the use of localStorage ", click enter, search on it, and there are all kinds of explanations.
then, there is nothing wrong with what is said upstairs.
what I want to say is that when you encounter a problem, you first want to solve it by yourself. You can't solve it by Baidu. Generally, you can reach Baidu.
like sf, I think you have time to edit this problem in the editor. Baidu search has solved it.
if it can't be solved, wouldn't it be better to ask sf again?


var a = {
    name: "silence",
    age: "18"
}
localstroage.setItem('info', JSON.stringify(a)); // 
var name = JSON.parse(localstroage.getItem('info')).name; // 

as said on the first floor, first of all, no matter what type of data is stored in localStroage, it is stored in the form of key,value, value is stored as a string, and when it is taken out, it is taken out as key, for example:

const userInfo = {
    age:22,
    name:'xxx'
}

:localStroage.setItem('user',JSON.stringify(userInfo))
:JSON.parse(localstroage.getItem('user'));

this is the kind of problem that can be solved by asking Google Baidu and reading documents. Check out MDN . MDN LocalStorage ,
answered similar questions before, and
answered the question" how to log in to localStorage in vue "at codeshelper.
link: https://codeshelper.com/q/10..

using localStorage's JS object form is sometimes more flexible.

localStorage knowledge points:
JS object
read form:
localStorage.name
add / modify
localStorage.name = "xuanyuan"
where "xuanyuan" can only be in the form of a string (so far only strings are supported). Therefore, when storing a JSON object, you need to execute JSON.stringify, so when you get it, you need to execute JSON.parse
delete
detele localStorage.name

.

API
get the number of key-value pairs
localStorage.length
read
localStorage.getItem ('name'), localStorage.key (i)
add / modify
localStorage.setItem (' name','xuanyuan')
delete corresponding key
localStorage.removeItem ('name')
Delete all data
localStorage.clear ()

by the way, the localStorage validity period is permanent. The average browser can store around 5MB. SessionStorage api is the same as localStorage. The default validity of
sessionStorage is the browser's session time (that is, it disappears when the tab is closed).
localStorage scope is protocol, hostname, port. (in theory, it is always in the device without artificial deletion.)
sessionStorage scope is window, protocol, hostname, port.


all the data saved in localstroage will be converted into a string, and it will also be a string when it is obtained. The string can be transferred to json

.
Menu