Json.stringify () and json.parse () are required to serialize and deserialize complex arrays and objects in the local storage. Encapsulate the storage simply, so that the value type taken out is consistent with the storage, and the entire object can also be stored.
1, install,
npm install pz_storage --save
Copy the code
2, use,
import storage from 'pz_storage'; // Store a single value storage.set('name'.'esther'); // Get a single value storage.get('name'); // Delete a single value storage.remove('name'); // Clear all storages.clear (); // Store the object in the key-value form storage.setList({a:'1',
b: '2.1',
c: ['a'.'b'.'c'],
d: {
'd-1': 'd-1'.'d-2': 'd-2'
},
e: true,
f: new Date(),
g: function(){ console.log(111); }}); // Get multiple values, pass in the array form key storage.getList(['a'.'b'.'d'.'f']); // Remove multiple values and pass in the array form key storage.removeList(['a'.'b'.'d'.'f'] // sessionStorage, API same as storage.session.set('name'.'esther');
Copy the code
You can also import storage.js from the dist folder directly into the HTML page
<script src="dist/storage.js"></script>
Copy the code
Making the address