简体   繁体   中英

how to store data in local storage in angular2

login(user) {
    this.loginService.login(user).subscribe((resp) => {
      console.log(resp);
    }, (err) => {
      console.log(err);
    });
  }

In this in response I'm getting:

Response {_body: "{"success":{"data":{"displayName":"yash gupta","id…9.Q_jhs4sEA7dOxsdMIG0R5X8U0hW9-5ZekI6kWX265iU"}}}", status: 200, ok: true, statusText: "OK", headers: Headers, …}
headers
:
Headers {_headers: Map(1), _normalizedNames: Map(1)}
ok
:
true
status
:
200
statusText
:
"OK"
type
:
2
url
:
"http://localhost:3234/api/login/default"
_body
:
"{"success":{"data":{"displayName":"yash gupta","id":258,"email":"asdasdasd@asdasd.com","role":"admin","token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkaXNwbGF5TmFtZSI6Inlhc2ggZ3VwdGEiLCJpZCI6MjU4LCJlbWFpbCI6Inlhc2hAYWtlby5ubyIsInJvbGUiOiJhZG1pbiIsImlhdCI6MTUxNTU2NjA3NCwiZXhwIjoxNTE1NjUyNDc0LCJhdWQiOiJCdWxsVG9rZW5Vc2VycyIsImlzcyI6Imh0dHBzOi8vYnVsbFRva2VuQXBpLmNvbSJ9.Q_jhs4sEA7dOxsdMIG0R5X8U0hW9-5ZekI6kWX265iU"}}}"
__proto__
:
Body

I only need to store token in my local storage of browser. Can someone tell me how can I store using set item method.

Use HTML 5 LocalStorage,

//Set

localStorage.setItem("token", Response.token);

//Get

let token = localStorage.getItem("token");

FYI: If you want to see the stored variable follow these steps(in chrome browser),

1) take developer tool (By clicking F12)

2) Go to Application tab

3) Open Local Storage menu from Storage area

4) Click your site listed

Now you can see your variable name ( token )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM