vue js store and retrieve api data to localstorage 1

vue js store and retrieve api data to localstorage

.then((res) => {
     localStorage.setItem("apiData", JSON.stringify(res.data));
});

//////// THEN YOU CAN GET DATA BY
var data = JSON.parse(localStorage.getItem("apiData"));

Here is what the above code is Doing:
1. We’re using the axios library to make a GET request to the API.
2. We’re using the .then() method to wait for the response from the API.
3. We’re using the localStorage.setItem() method to save the data to localStorage.
4. We’re using the JSON.stringify() method to convert the data to a string.
5. We’re using the JSON.parse() method to convert the string back to an object.
6. We’re using the localStorage.getItem() method to get the data from localStorage.

Similar Posts