https://www.jianshu.com/p/6e1bacd35f59
https://www.jianshu.com/p/60c13168cc8f
一、安裝
npm install js-cookie --save
二、引用
import Cookies from 'js-cookie'
三、一般使用
- 存到Cookie去
-
// Create a cookie, valid across the entire site:
-
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: '' });
-
-
// name value
-
Cookies.set('TokenKey', token, { expires: 1000, path: '/', domain: 'xx.com' });
-
-
-
//不寫過期時間,默認為1天過期
-
this.$cookies.set("user_session","25j_7Sl6xDq2Kc3ym0fmrSSk2xV2XkUkX")
-
-
this.$cookies.set("token","GH1.1.1689020474.1484362313","60s"); // 60秒后過去
-
-
this.$cookies.set("token","GH1.1.1689020474.1484362313","30MIN"); // 30分鍾后過去
2.在Cookie中取出
-
// Read cookie:
-
Cookies.get('name'); // => 'value'
-
Cookies.get('nothing'); // => undefined
-
-
// Read all visible cookies:
-
Cookies.get(); // => { name: 'value' }
3.刪除
-
// 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!
四、特殊使用(在Cookie中存對象)
跟一般使用不同的是,從Cookie中取出的時候,要從字符串轉換成json格式:
-
const user = {
-
name: 'lia',
-
age: 18
-
}
-
Cookies.set('user', user)
-