安装依赖
npm i js-cookie
引入
import Cookies from 'js-cookie'
存入
创建cookie
Cookies.set('name', 'value');
设置期限7天有效期
Cookies.set('name', 'value', { expires: 7 });
对当前页面的路径有效
Cookies.set('name', 'value', { expires: 7, path: '' });
读取cookie
Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined
// 获取全部
Cookies.get(); // => { name: 'value' }
删除
// Delete cookie:
Cookies.remove('name');
// Delete a cookie valid to the path of the current page:
Cookies.set('name', 'value', { path: '' });
Cookies.remove('name'); // fail!
Cookies.remove('name', { path: '' }); // removed!
Cookies.remove('name', { path: '', domain: '' }); // removed!
js-cookie npm文档
js-cookie npm文档