js-cookie
GitHub地址
js-cookie
具体使用
js-cookie插件是一个JS操作cookie的插件,源文件只有3.34 KB,非常轻量级,js-cookie也支持npm和Bower安装和管理,下面看看js-cookie的具体用法
1、引入js-cookie.js
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.模块化开发时:
$ npm install js-cookie --save
import Cookies from 'js-cookie'
2、js-cookie.js常用的API和方法
a、设置cookie
Cookies.set('name', 'value', { expires: 7, path: '' });//7天过期
Cookies.set('name', { foo: 'bar' });//设置一个json
b、读取cookie
Cookies.get('name');//获取cookie
Cookies.get(); #读取所有的cookie
c、删除cookie
Cookies.remove('name'); #删除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!