【问题标题】:how to store data in angular 5 localstorage如何将数据存储在 Angular 5 本地存储中
【发布时间】:2018-12-28 00:10:14
【问题描述】:
userData = {
    items: [
      { medicine_id: "", medicine_qty: 1, priscription_path: "" }
     { medicine_id: "", medicine_qty: 1, priscription_path: "" }
     { medicine_id: "", medicine_qty: 1, priscription_path: "" }
    ],
    user_id: "",
    store_id: "",
  };

我想使用 Angular 5 将这样的数据存储在我的本地存储中。使用接口类

【问题讨论】:

标签: arrays json ionic3 angular5 local-storage


【解决方案1】:

在您的示例中,由于 userData 是一个对象,因此您必须首先使用 JSON.stringify 将其转换为字符串,然后存储在本地存储中。

localStorage.getItem('userData',JSON.stringify(userData))
var user = JSON.parse(localStorage.getItem('userData'));

那么转换后就可以使用了:

将项目存储在本地存储中:

localStorage.setItem('item_name','content');

检索存储的项目(使用键名):

localStorage.getItem('item_name');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2013-08-17
    • 2019-01-03
    相关资源
    最近更新 更多