- Cookie LocalStorage SessionSto
- cookie 、localStorage 、sessionSto
- 关于cookie,localStorage,sessionSto
- 简述cookie、localStorage、sessionSto
- 理解cookie、localStorage、sessionSto
- 浅谈Cookie、LocalStorage、SessionSto
- cookie、localStorage 和 sessionSto
- 探究Cookie、LocalStorage与SessionStr
- Cookie、sessionStorage、localStora
- 8-10 localstorage和Vuex的其他使用
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');
网友评论