1 NPM module

cnpm i js-cookie --save
Copy the code

2 the CDN file

<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
Copy the code
// Set key-value pairs
Cookies.set('name'.'value');
 // Set the validity period to 7 days
Cookies.set('name'.'value', { expires: 7 });
 // Set the key-value pair date and path
Cookies.set('name'.'value', { expires: 7.path: ' ' });
 / / read
Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined
 // Read all stored values
Cookies.get(); // => { name: 'value' }
 // Delete a storage
Cookies.remove('name');
 // Delete the value matching a path
Cookies.set('name'.'value', { path: ' ' });
Cookies.remove('name'); // fail!
Cookies.remove('name', { path: ' ' }); // removed!
/ / store
 Cookies.set('name', { foo: 'bar' });
/ / read
Cookies.getJSON('name'); // => { foo: 'bar' }
Cookies.getJSON(); // => { name: { foo: 'bar' } }
Copy the code