A minimalistic Vue.js v2.x plugin for web storage
- Choose either
localStorage
orsessionStorage
- Prefix all of your stored keys
- Auto
JSON.stringify
andJSON.parse
- Events for cross tab communication
# npm
npm install vue-web-storage --save
# Yarn
yarn add vue-web-storage
import Vue from 'vue';
import Storage from 'vue-web-storage';
Vue.use(Storage);
Vue.use(Storage, {
prefix: 'your_app_name',// default `app_`
driver: 'session', // default 'local'
})
All methods takes care of prefix
in key name, so you no need to specify key prefix when using them.
Stores the value
under specified key
in storage. Convert value to JSON before saving.
Returns true
on success and false
on errors.
Vue.$storage.set('name', 'john')
Vue.$storage.set('isAdmin', true)
Vue.$storage.set('roles', ['admin', 'sub-admin'])
Vue.$storage.set('permission', {id: 2, slug: 'edit_post'})
Retrieves given key
value from storage, parse the value from JSON before returning.
If parsing failed then returns the actual value get from storage.
Vue.$storage.get('name')
Removes the key
from storage.
Vue.$storage.remove('name')
Removes all keys from storage. Passing true
will clear whole storage without taking prefix
into consideration.
Vue.$storage.clear()
Returns array of keys stored in storage. Passing true
will return prefixed key names.
Vue.$storage.keys()
Returns true
if key exists in storage regardless of its value.
Vue.$storage.hasKey('name')
Returns the number of keys stored in storage.
Vue.$storage.length()
- These are not regular Vue.js events, these events to be used for cross tab communication.
Attaches a listener method to the given key. You can attach multiple methods on the same key.
const onChangeName = (newValue, OldValue, url) => {
// do something when `name` value gets changed
};
Vue.$storage.on('name', onChangeName);
Vue.$storage.on('name', this.anotherMethod)
Removes specified listener method form the given key.
Vue.$storage.off('name', this.onChangeName)
- Removes all listeners for the given key otherwise clears the listeners pool when not specified.
Vue.$storage.clearEvents('name');
Vue.$storage.clearEvents()
<!-- Vue js -->
<script src="https://unpkg.com/vue@2.5/dist/vue.min.js"></script>
<!-- Lastly add this package -->
<script src="https://unpkg.com/vue-web-storage@2"></script>
<!-- Init the plugin -->
<script>
Vue.use(VueWebStorage)
</script>
- This package is using Jest for testing
- Tests can be found in
__test__
folder. - Execute tests with this command
yarn test
Please see CHANGELOG for more information what has changed recently.
MIT License