0

My application becomes slow because I make too many calls to the API to retrieve information. I would like to create a large object with all the information shared with all the pages and get them from localStorage so that the application is more fluid. I work in Angular 7 with Ionic 4. thank you in advance.

2 Answers2

0

Storing everything in localStorage seems to be a bad idea, localstorage stores stringified objects(so you will loose methods, if any. So you will then need an added task to instantiate the Objects again with the data in localStorage).

Why do you want to save in localStorage? You want to persist data in page reloads too? If not then you can use shared services and if yes and If you have a determined point on when you will update the saved data then you can use service workers to cache the data.

Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
0

You can use localStorage but it accepts values only in the form of string, so you'll have to serialise your data before saving it. Have you considered making your app a PWA? You can do a whole lot of caching to make your application a lot faster (including the load times by pre-caching your files on the browser).

But nonetheless, if you are persistent on localStorage you could do:

var data = [{"test": {}}]; // Some json data
localStorage.setItem(<KEY NAME>, JSON.stringify(data));

// To retrieve it
var saved = localStorage.getItem(<KEY NAME>)
console.log(JSON.parse(saved));
varun agarwal
  • 1,491
  • 6
  • 9