Cookie LocalStorage SessionSto
2018-04-09 本文已影响0人
页面仔小杨
Cookie LocalStorage SessionStorage对比
三者异同
操作
localStorage和sessionStorage都具有相同的操作方法,例如setItem、getItem和removeItem等
set方法:
setStore (name, content) {
if (!name) return
if (typeof content !== 'string') {
content = JSON.stringify(content)
}
window.localStorage.setItem(name, content)
}
get方法:
getStore (name) {
if (!name) return
return JSON.parse(window.localStorage.getItem(name))
}
remove方法:
removeStore (name) {
if (!name) return
window.localStorage.removeItem(name)
}
cookie操作
由于js原生cookie接口不友好,建议使用js-cookie
$ npm install js-cookie --save
import Cookies from 'js-cookie'
Cookies.set('name', 'value', { expires: 365 });
Cookies.get('name');
Cookies.remove('name');