vue 中 直接操作 cookie 及 如何使用工具 js-cookie
2023-09-11 14:19:39 时间
使用 js-cookie 工具:(比较方便,推荐使用)
工具地址:https://www.npmjs.com/package/js-cookie
安装
npm i js-cookie
引入
import Cookies from 'js-cookie'
具体使用见以下:
Basic Usage
Create 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'); // => undefined
Read 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(此方法只能清除简单的cookie 如果带路径,则不能清除)
function clearAllCookie() { var keys = document.cookie.match(/[^ =;]+(?=\=)/g); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } }
相关文章
- vue.js--基础 事件结合双向数据绑定实现todolist 待办事项 已经完成 和进行中,键盘事件
- node.js 调用天气webservice接口
- JQ,js给所有table元素标签添加样式类,并且外面再套一个div
- 【Vue/js】Js中执行变量中的命令语句,也就是所谓的宏替换(很实用的例子)
- 29dwr - engine.js 功能(选项索引)
- 微信小程序 - 父组件调用子组件的函数方法(像 Vue.js 中 ref 属性一样,直接 this.$refs.xx 获取子组件数据和方法)详细教程示例代码
- js定时器的时间最小值-setTimeout、setInterval
- 【HarmonyOS】【JS】【布局】鸿蒙js开发input 输入框弹出输入法时上方布局被挤扁?
- JS 字符串切割成数组
- Vue项目 跨域 解决方案与 vue.config.js 配置解析
- Vue.js 组件编码规范
- 用 vue cli 脚手架搭建单页面 Vue 应用(进阶2)
- [js高手之路] html5 canvas动画教程 - 匀速运动
- [js高手之路] vue系列教程 - 绑定class与行间样式style(6)
- [js高手之路] vue系列教程 - 实现留言板todolist(3)
- npm安装vue-cli报错internal/modules/cjs/loader.js(Error: Cannot find module 'D:Programnodejsnode_globalnode_modulesvue-clibinvue')
- Vue.js中 watch的理解以及深度监听
- Vue.js style (内联样式)
- three.js 测试1
- Three.js Example 注解 —— hilbert2D.js