VueLocalStore
vue localStorage plugin with prefix
, unique id
injectable for vue2.x which can control version by version_name
feature.
Install
VueLocalStore is an umd module, you can easily use by cdn or es6 module
$ npm install @johnnywang/vue-localstore
Basic Introduction
create instance
Same as vue-router, create localStore instance
import Vue from 'vue';
import VueLocalStore from '@johnnywang/vue-localstore';
Vue.use(VueLocalStore);
const localStore = new VueLocalStore({
versionName: 'test_v0.0.1',
versionNameKey: 'version_name',
eventDataKey: 'event_data',
prefix: '',
uid: 'anonymous',
});
export default localStore;
inject to vue intance
import Vue from 'vue';
import localStore from './localStore';
new Vue({
localStore,
}).$mount('#app');
access in component
export default {
mounted() {
console.log(this.$localStore);
},
};
API Introduction
setUid(uid)
set prefixed uid content, uid
will be concat as bellow:
params
- uid
- type:
string
- default:
anonymous
const prefixedKey = `${prefix}${uid}_${key}`;
demo
export default {
created() {
this.$localStore.setUid('good_uid_30678444');
},
};
setRoot(key, value)
set localstorage to root key(real localstorage)
params
- key
- value
demo
export default {
created() {
this.$localStore.setRoot('name', 'Johnny');
},
};
getRoot(key)
get localStorage root key's value
params
- key
demo
export default {
created() {
this.$localStore.getRoot('name');
},
};
removeRoot(key)
remove localStorage root key
params
- key
demo
export default {
created() {
this.$localStore.removeRoot('name');
},
};
set(key, value)
set event data key with value, the key will be prefixed.
params
- key
- type:
string
- desc: will be auto prefixed before editing
- value
demo
export default {
created() {
this.$localStore.set('name', 'Johnny');
},
};
get(key)
get event data key's value, the key will be prefixed.
params
- key
- type:
string
- desc: will be auto prefixed before editing
demo
export default {
created() {
this.$localStore.set('name', 'Johnny');
},
};
remove(key)
remove event data key, the key will be prefixed.
params
- key
- type:
string
- desc: will be auto prefixed before editing
demo
export default {
created() {
this.$localStore.remove('name');
},
};
Configs
versionName(required)
version_name for this content
versionNameKey(optional, default: 'version_name')
localStorage key name for storing version_name
eventDataKey(optional, default: 'event_data')
localStorage key name for storing event_data
prefix(optional, default: '')
prefix value for event_data object's property
uid(optional, default: 'anonymous')
inject uid to seperate user or other usage, can be modified later by setUid
api.