VUE浏览器储存封装
import {isFunction, extend} from 'lodash'
const _originStorage = function () {
var pluses = /\+/g
function encode (s) {
return _cookie.raw ? s : encodeURIComponent(s)
}
function decode (s) {
return _cookie.raw ? s : decodeURIComponent(s)
}
function stringifyCookieValue (value) {
return encode(_cookie.json ? JSON.stringify(value) : String(value))
}
function parseCookieValue (s) {
if (s.indexOf('"') === 0) {
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\')
}
try {
s = decodeURIComponent(s.replace(pluses, ' '))
return _cookie.json ? JSON.parse(s) : s
} catch (e) {}
}
function read (s, converter) {
var value = _cookie.raw ? s : parseCookieValue(s)
return isFunction(converter) ? converter(value) : value
}
var _cookie = function (key, value, options) {
// Write
if (arguments.length > 1 && !isFunction(value)) {
options = extend({}, _cookie.defaults, options)
if (typeof options.expires === 'number') {
var days = options.expires
var t = options.expires = new Date()
t.setMilliseconds(t.getMilliseconds() + days * 864e+5)
}
return (document.cookie = [
encode(key), '=', stringifyCookieValue(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''))
}
var result = key ? undefined : {}
var cookies = document.cookie ? document.cookie.split('; ') : []
var i = 0
var l = cookies.length
for (; i < l; i++) {
let parts = cookies[i].split('=')
let name = decode(parts.shift())
let cookie = parts.join('=')
if (key === name) {
result = read(cookie, value)
break
}
if (!key && (cookie = read(cookie)) !== undefined) {
result[name] = cookie
}
}
return result
}
// originStorage
return (function () {
var cookieKeyPrefix = '__localStorage__'
var supportLocalStorage = true
try {
window.localStorage.setItem('__test__', 1)
window.localStorage.getItem('__test__')
window.localStorage.removeItem('__test__')
} catch (e) {
supportLocalStorage = false
}
if (supportLocalStorage) {
return {
get: function (key) {
return window.localStorage.getItem(key)
},
set: function (key, value) {
return window.localStorage.setItem(key, value)
},
clear: function () {
return window.localStorage.clear()
},
remove: function (key) {
return window.localStorage.removeItem(key)
}
}
} else {
return {
get: function (key) {
return _cookie(cookieKeyPrefix + key)
},
set: function (key, value) {
return _cookie(cookieKeyPrefix + key, value, {
expires: 365
})
},
clear: function () {
var cookies = document.cookie.split(';')
for (var i = 0; i < cookies.length; i++) {
var key = cookies[i].split('=')[0]
if (key.indexOf(cookieKeyPrefix) === 0) {
_cookie(key, '', {
expires: -1
})
}
}
},
remove: function (key) {
return _cookie(cookieKeyPrefix + key, '', {
expires: -1
})
}
}
}
})()
}
// export default () => {
const _localStorage = function () {
const originStorage = _originStorage()
this.defaultOption = {
maxAge: 0 // ms
}
this.get = function (k) {
var self = this
var a = originStorage.get(k)
var _return
try {
var json = JSON.parse(a)
var expiresTimestamp = json.e
if (expiresTimestamp && Date.now() > expiresTimestamp) {
_return = undefined
self.remove(k)
} else {
_return = json.a
}
} catch (e) {
_return = undefined
}
return _return
}
this.set = function (k, v, option) {
option = extend(true, extend({}, this.defaultOption), option)
var o = {
a: v,
e: option.maxAge > 0 ? (Date.now() + option.maxAge) : 0
}
originStorage.set(k, typeof o === 'string' ? o : JSON.stringify(o))
}
this.clear = function () {
return originStorage.clear()
}
this.remove = function (k) {
return originStorage.remove(k)
}
}
export default new _localStorage()
挂载
// localStorage plugin
import localStorage from '@/common/localstorage'
function plugin (Vue) {
if (plugin.installed) {
return
}
Vue.localStorage = localStorage
Object.defineProperties(Vue.prototype, {
$localStorage: {
get () {
return localStorage
}
}
})
}
if (typeof window !== 'undefined' && window.Vue) {
window.Vue.use(plugin)
}
export default plugin
引入import LocalStorage from '@/plugin/localStorage';
Vue.use(LocalStorage);
VUE浏览器储存封装的更多相关文章
- vue2.0 如何自定义组件(vue组件的封装)
一.前言 之前的博客聊过 vue2.0和react的技术选型:聊过vue的axios封装和vuex使用.今天简单聊聊 vue 组件的封装. vue 的ui框架现在是很多的,但是鉴于移动设备的复杂性,兼 ...
- Vue之优化封装请求方法
Vue之优化封装请求方法 对于代码中的请求操作 1.接口请求可能需要重用 2.实际工作中,接口非常容易变动, 改起来很麻烦! 我们建议的做法是把所有的请求都封装成函数然后统一的>###组织到模块 ...
- vue自定义插件封装,实现简易的elementUi的Message和MessageBox
vue自定义插件封装示例 1.实现message插件封装(类似简易版的elementUi的message) message组件 <template> <transition ...
- 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- Vue服务端渲染和Vue浏览器端渲染的性能对比
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- vue项目-axios封装、easy-mock使用
vue全家桶概括下来就是 项目构建工具(vue-cli) 路由(vue-router) 状态管理(vuex) http请求工具 vue有自己的http请求工具插件vue-resource,但是vue2 ...
- android利用WebView实现浏览器的封装
android提供了封装浏览器的接口,可以让开发者利用自己的view显示网页内容.今天又实现研究了一下,利用WebView显示浏览器内容,还可以利用 WebViewClient显示自己需要的内容. 参 ...
- 原生JS跨浏览器事件封装处理
引子:用javascript给元素绑定事件,我们可以用addEventListener这个方法,然而这个方法有兼容问题,比如在IE浏览器上面就无效,在IE上面要用attachEvent这个方法 一.a ...
随机推荐
- Elasticsearch修改network后启动失败
修改 /config/elasticsearch.yml(我的安装目录是:/var/www/elasticsearch-6.4.2/elasticsearch-6.4.2), network.host ...
- mock测试
看到群里有人说mock测试,究竟什么是mock测试呢?开始自己也没明白,查了下相关资料.还是很有必要了解哈:那么mock测试能解决什么问题?mock测试要如何做呢?今天为大家做简单介绍.mock测试就 ...
- mybatis基础(上)
框架图 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory 将SqlSessionFactoryBui ...
- 利用MingW检验程序运行内存
今天zhx老师在讲课的时候提到了一种检验程序内存的方法 一般计算内存的方法就是手算,手动计算代码中每个变量所占的内存然后加起来 具体可以参考这篇文章 zhx老师讲的方法可以实现全自动化计算内存 具体怎 ...
- mysql innodb存储引擎和一些参数优化
mysql 的innodb存储引擎是事务性引擎,支持acid.innodb支持版本控制和高并发的技术是svcc:需要重点注意:myisam只缓存索引,innodb缓存索引和数据:
- Python开发爬虫之BeautifulSoup解析网页篇:爬取安居客网站上北京二手房数据
目标:爬取安居客网站上前10页北京二手房的数据,包括二手房源的名称.价格.几室几厅.大小.建造年份.联系人.地址.标签等. 网址为:https://beijing.anjuke.com/sale/ B ...
- Linux 安装 powershell
linux 安装 powershell Intro powershell 已经推出了一个 Powershell Core, 版本号对应 Powershell 6.x,可以跨平台,支持 Linux 和 ...
- ASP.NET Core 入门教程 5、ASP.NET Core MVC 视图传值入门
一.前言 1.本教程主要内容 ASP.NET Core MVC 视图引擎(Razor)简介 ASP.NET Core MVC 视图(Razor)ViewData使用示例 ASP.NET Core MV ...
- centos7查看可登陆用户
一.命令 cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1 cat /etc/passwd | grep /bin/bash | cu ...
- SQL SERVER 索引碎片
一次发现同样的SQL在线上库和复制库执行时间差好多,重新创建相关表索引,性能提升明显,怀疑索引有碎片