vue-ls-xq
Version:
Vue localstorage.From git+https://github.com/RobinCK/vue-ls.git,but change some code
76 lines (63 loc) • 1.87 kB
JavaScript
import { MemoryStorage, WebStorage } from './storage';
// eslint-disable-next-line
const _global = (typeof window !== 'undefined' ? window : global || {});
/**
* @type {{install: (function(Object, Object): WebStorage)}}
*/
const VueStorage = {
/**
* Install plugin
*
* @param {Object} Vue
* @param {Object} options
* @returns {WebStorage}
*/
install(Vue, options = {}) {
const _options = Object.assign({}, options, {
storage: options.storage || 'local',
name: options.name || 'ls',
});
if (_options.storage && ['memory', 'local', 'session'].indexOf(_options.storage) === -1) {
throw new Error(`Vue-ls: Storage "${_options.storage}" is not supported`);
}
let store = null;
switch(_options.storage) { // eslint-disable-line
case 'local':
store = 'localStorage' in _global
? _global.localStorage
: null
;
break;
case 'session':
store = 'sessionStorage' in _global
? _global.sessionStorage
: null
;
break;
case 'memory': store = MemoryStorage;
break;
}
if (!store) {
store = MemoryStorage;
// eslint-disable-next-line
console.error(`Vue-ls: Storage "${_options.storage}" is not supported your system, use memory storage`);
}
const ls = new WebStorage(store);
ls.setOptions(Object.assign(ls.options, {
namespace: '',
}, _options || {}));
Vue[_options.name] = ls; // eslint-disable-line
Object.defineProperty(Vue.prototype, `$${_options.name}`, {
/**
* Define $ls property
*
* @return {WebStorage}
*/
get() {
return ls;
},
});
},
};
_global.VueStorage = VueStorage;
export default VueStorage;