如何以angular2

时间:2018-01-10 06:37:30

标签: angular local-storage angular2-services

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

作为回应,我得到了:

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

我只需要在我的浏览器本地存储中存储令牌。有人能告诉我如何使用set item方法存储。

1 个答案:

答案 0 :(得分:1)

使用 HTML 5 LocalStorage,

//设置

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

//获取

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

仅供参考:如果您想查看存储的变量,请按照以下步骤操作(在Chrome浏览器中),

1)采用开发者工具(点击F12)

2)转到“应用程序”选项卡

3)从存储区域打开本地存储菜单

4)点击您列出的网站

现在您可以看到您的变量名称(令牌