cookie的作用无需多言,自己封装一个cookie,不停地复制黏贴页颇为麻烦,在这里,有npm为我们封装好的插件js-cookie: https://www.npmjs.com/package/js-cookie。
引用方法:
1.直接饮用cdn:<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
2.本地下载下来后:<script src="/path/to/js.cookie.js"></script>
3.模块化开发时: import Cookies from ‘js-cookie‘
使用方法:
Basic UsageCreate a cookie, valid across the entire site:Cookies.set(‘name‘, ‘value‘);Create a cookie that expires 7 days from now, valid across the entire site:Cookies.set(‘name‘, ‘value‘, { expires: 7 });Create an expiring cookie, valid to the path of the current page:Cookies.set(‘name‘, ‘value‘, { expires: 7, path: ‘‘ });Read cookie:Cookies.get(‘name‘); // => ‘value‘Cookies.get(‘nothing‘); // => undefinedRead all visible cookies: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!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
js-cookie的用法
原文地址:https://www.cnblogs.com/gluncle/p/9639073.html
知识推荐
- 关于JS闭包
- ieDriver启动IE浏览器显示This is the initial start page for the WebDriver server的解决办法
- [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
- Jquery | 基础 | 慕课网 | 类选择器
- JS授权
- Jsp指令
- nodejs返回接口给前端
- Angular待办事项应用4——模拟web服务
- 13-JS中的面向对象
- jQuery Ajax 操作函数
- jQuery ?--计算复选框被选中个数
- js关于“call方法”百度,阿里超难面试题
- CSS特效(8)——单行居中,多行居左,超过两行用省略号(绝对定位)
- 几种流行的AJAX框架对比:Jquery,Mootools,Dojo,ExtJs,Dwr
- html5中新的表单
- css3 线性渐变和径向渐变
- 使用 dotTrace 分析 .NET Core 代码问题
- .NET Core部署中你不了解的框架依赖与独立部署