A minimalistic Vue.js plugin for web storage
Vue.js version | Package version | Branch |
---|---|---|
2.x | 5.x | 5.x |
3.x | 6.x | master |
- Choose either
localStorage
orsessionStorage
or both - Prefix all of your stored keys
- Auto
JSON.stringify
andJSON.parse
- Events for cross tab communication
# yarn
yarn add vue-web-storage
# npm
npm install vue-web-storage
import {createApp} from 'vue';
import StoragePlugin from 'vue-web-storage';
const app = createApp({}).mount('#app')
app.use(StoragePlugin);
// Use in your components
// this.$localStorage
app.use(StoragePlugin, {
prefix: 'your_app_slug_',// default `app_`
drivers: ['session', 'local'], // default 'local'
});
// It will register two different instances
// this.$sessionStorage
// this.$localStorage
All methods take care of prefix
in key name, so you no need to specify the prefix when using them.
Stores the value
under specified key
in storage. Convert value to JSON before saving. This method throws error on
failure.
this.$localStorage.set('name', 'john')
this.$localStorage.set('isAdmin', true)
this.$localStorage.set('roles', ['admin', 'sub-admin'])
this.$localStorage.set('permission', {id: 2, slug: 'edit_post'})
Retrieves given key
value from storage, parse the value from JSON before returning. If parsing failed then throws
error.
this.$localStorage.get('name')
this.$localStorage.get('doesNotExistsInStorage', 'defaultValue')
Removes the individual key
from storage.
this.$localStorage.remove('name')
Removes all keys from storage. Passing true
will clear whole storage without taking prefix
into consideration.
this.$localStorage.clear()
Returns array of keys stored in storage. Passing true
will return prefixed key names.
this.$localStorage.keys()
Returns true
if key exists in storage regardless of its value.
this.$localStorage.hasKey('name')
Returns the number of keys stored in storage.
this.$localStorage.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, originUrl) => {
// do something when `name` value gets changed
};
this.$localStorage.on('name', onChangeName);
this.$localStorage.on('name', this.anotherMethod)
Removes specified listener method form the given key.
this.$localStorage.off('name', this.onChangeName)
- Removes all listeners for the given key otherwise clears the listeners pool when key not specified.
this.$localStorage.clearEvents('name');
this.$localStorage.clearEvents()
<!-- Vue js -->
<script src="https://cdn.jsdelivr.net/npm/vue@3"></script>
<!-- Lastly add this package -->
<script src="https://cdn.jsdelivr.net/npm/vue-web-storage@6"></script>
<!-- Init the plugin -->
<script>
yourApp.use(VueWebStorage.default)
</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