vue中操作cookie的插件
js-cookie
安装:
npm i js-cookie
import Cookies from 'js-cookie'
具体用法:
写入:
Cookies.set('name', 'value'); Create a cookie that expires 7 days from now, valid across the entire site: Cookies.set('name', 'value', { expires: 7 });
创建一个过期的cookie,该cookie对当前页的路径有效:
Cookies.set('name', 'value', { expires: 7, path: '' });
获取:
Cookies.get('name'); // => 'value' Cookies.get('nothing'); // => undefined 获取所有cookie Cookies.get(); // => { name: 'value' }
删除: Cookies.remove('name');
删除对当前页路径有效的cookie: Cookies.set('name', 'value', { path: '' }); Cookies.remove('name'); //直接删除 失败 Cookies.remove('name', { path: '' }); // 删除成功! IMPORTANT! when deleting a cookie, you must pass the exact same path and domain attributes that was used to set the cookie, unless you're relying on the default attributes. Note: Removing unexisting cookie does not raise any exception nor return any value