javascript - localstorage.setitem 不适用于 Angular

标签 javascript angularjs json local-storage

下面是我的函数。我可以通过 res 在 console.log 中看到一个对象:

login2(){
    console.log(this.user_form_value);
    console.log(this.password_form_value);
    this._loginService.login(this.user_form_value, this.password_form_value).subscribe(
      res => console.log(res.user),
      res2 =>localStorage.setItem("getLoggedInUserANG1",JSON.stringify(res2.user))
      )

  }

但是当我尝试将相同的对象存储在本地存储中时,它没有被保存。我是 Angular 新手,但没有错误,我不知道为什么它没有被保存。

保存后,我还想在数组变量中使用 getitem 获取相同的内容并将其保存在 console.log 中。我想从基础上了解问题以及我到底错在哪里。

下面是我的console.log的屏幕截图

enter image description here

最佳答案

res2 未定义。我相信你需要看看JavaScript Arrow Funcions docs .

尝试一下:

login2() {
    console.log(this.user_form_value);
    console.log(this.password_form_value);
    this._loginService.login(this.user_form_value, this.password_form_value).subscribe(
        res => {
            console.log(res.user);
            localStorage.setItem("getLoggedInUserANG1",JSON.stringify(res.user));
        }
    )
}

关于javascript - localstorage.setitem 不适用于 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44143066/

相关文章:

Javascript 循环内部闭包

javascript - 取消选中复选框时删除值

javascript - 为 jquery 添加 Rails 输入名称时遇到问题

javascript - 如何使用 AngularJS 从 Json 中检索数据?

javascript - 如何使用 lodash 有效地合并两个日期对象数组

javascript - 如何使用 sinon 在被测函数内 stub 类方法?

AngularJs - 我应该如何分离模式窗口?

带有条件表达式的 AngularJS ng 风格

ios - 使用 Swift 访问 JSON

javascript - 如何创建动态 html 表单的创建 JSON 对象(表单元素)?