需要存储对象,以便在刷新时,对象应该在angularJS中可用

Need to store Objects, so that on refresh, the object should be available in angularJS?

本文关键字:对象 angularJS 刷新 存储      更新时间:2023-09-26

我已经实现了一个服务。我要创建两个对象。刷新后,它们的值变为null。

我应该如何存储这些对象,以便我也可以在刷新后访问它们。

var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(xyz);
var cognitoUser = AWSCognito.authenticate(userpool);

是在服务中实现的,所以它只会运行一次。现在我需要存储这些对象,以便我可以在刷新后访问它们。

对于应用内的数据持久化,应该使用Services。

对于浏览器中的数据持久化,应该使用localstorage。

阅读localstorage: https://developer.mozilla.org/en/docs/Web/API/Window/localStorage

localstorage的angular库:https://npmjs.com/package/angular-local-storage

我更喜欢- localstorage选项。

您有几个选择:

  1. 将其存储在数据库中,每次应用程序第一次加载时(如刷新后)从DB中读取一次数据。
  2. 将其存储在localstorage中,并在应用程序启动时读取
  3. IndexDB -内部浏览器数据库(请注意支持指标)

使用javascript LocalStorage:

localStorage.setItem("userPool", userPool);
localStorage.setItem("cognitoUser", cognitoUser);

localStorage.getItem("userPool");
localStorage.getItem("cognitoUser");

你也可以使用angular localstorage库,它有更灵活的API。

角LocalStorage

最好的选择是使用angular本地存储。https://github.com/grevory/angular-local-storage/