地址

npm:https://www.npmjs.com/package/js-cookie
github:https://github.com/js-cookie/js-cookie

安装

npm install js-cookie —save
import Cookies from ‘js-cookie’

使用

(1)存

  1. // Create a cookie, valid across the entire site:
  2. Cookies.set('name', 'value');
  3. // Create a cookie that expires 7 days from now, valid across the entire site:
  4. Cookies.set('name', 'value', { expires: 7 });
  5. // Create an expiring cookie, valid to the path of the current page:
  6. Cookies.set('name', 'value', { expires: 7, path: '' });

(2)取

  1. // Read cookie:
  2. Cookies.get('name'); // => 'value'
  3. Cookies.get('nothing'); // => undefined
  4. // Read all visible cookies:
  5. Cookies.get(); // => { name: 'value' }

(3)删

  1. // Delete cookie:
  2. Cookies.remove('name');
  3. // Delete a cookie valid to the path of the current page:
  4. Cookies.set('name', 'value', { path: '' });
  5. Cookies.remove('name'); // fail!
  6. Cookies.remove('name', { path: '' }); // removed!

参考资料

https://www.jianshu.com/p/6e1bacd35f59