js-cookie
安裝:
npm i js-cookie
import Cookies from 'js-cookie'
具體用法:
寫入:
Cookies.set('name', 'value'); Create a cookie that expires 7 days from now, valid across the entire site: Cookies.set('name', 'value', { expires: 7 });
創建一個過期的cookie,該cookie對當前頁的路徑有效:
Cookies.set('name', 'value', { expires: 7, path: '' });
獲取:
Cookies.get('name'); // => 'value' Cookies.get('nothing'); // => undefined 獲取所有cookie Cookies.get(); // => { name: 'value' }
刪除: Cookies.remove('name');
刪除對當前頁路徑有效的cookie: Cookies.set('name', 'value', { path: '' }); Cookies.remove('name'); //直接刪除 失敗 Cookies.remove('name', { path: '' }); // 刪除成功! 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