js-cookie

安装

npm install js-cookie --save

引用

import Cookies from 'js-cookie'

使用

1、设置Cookie

// 创建一个cookie,在整个站点上都有效:
Cookies.set('name', 'value');

// 创建一个7天后过期的cookie,在整个站点上都有效:
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: '/' });

2、读取Cookie

// Read cookie:
Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined

// Read all visible cookies:
Cookies.get(); // => { name: 'value' }

3、删除Cookie

// 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!

4、读取值为数组、对象的Cookie

const value = { name: 'LLT', age: 18 }
Cookies.set('Info', value)
Cookies.getJSON('Info')						  // { name: 'LLT', age: 18 }

你可能感兴趣的:(随开笔记)