vue-the-storages
A Vue plugin that enhances localStorage/sessionStorage (supports vue2, vue3).
It allows two-way binding of storage data, multi-page data binding, storage event listener, and rich api (async/sync).
the-storages
Based on:Description
-
Vue plugins
-
Support Vue2, vue3, and Vue-Cli project
-
Multi-page data binding and sync.
-
Provide storage change event listeners.
-
Various calling methods.
-
Automatic JSON parsing.
-
Mainly written using es6 and proxy.
Simple Demos:
You can open multiple pages at the same time and experience data binding between multiple pages
Usage:
vue-the-storages
-
- install
npm i vue-the-storages
-
- use
main.js (registered)
// vue2Vue // vue3 (just use)
Default global names:
$localData (localStorage mirror object)
$local (localStorage proxy object)
$sessionData (sessionStorage mirror object)
$session (sessionStorage proxy object)
You can pass in options when registering to modify the global name:
// default, you can modifyconst vueTheStoragesOptions = localMirrorKey: '$localData' localStorageKey: '$local' sessionMirrorKey: '$sessionData' sessionStorageKey: '$session' options: vueModule: null strict: true mirrorOperation: false updateMirror: true // vue2Vue // vue3 (just use)
App.vue or any components (test)
{{ $localData }} {{ $local }} test
↑ The above is the usage of vue plugin
All APIs are completely consistent, you can refer to
Read Samples
Read Docs
the-storages
↓ Below is the description ofImport:
// npm i the-storages const mirror = // create localStorage; createSession is sessionStorageconst storage = mirror_prx consolestorageconsole
vue-the-storages
Vue-Cli:Vue (common):
See index.html for details (vue3). vue2.html, vue2_zh.html
<!-- storages test page for vue2 --> the-storages | vue2 the-storage | vue2 storageData (storage mirror object): {{ storageData }} storageProxyObj (storage proxy object): {{ storageProxyObj }} click me, set a message
Samples:
// set data to localStoragestorage // set a objectstorage // set a stringstoragetest3 = hello111: 'im an object too' // set a object // set multiple data to localStoragestorage // parameter [keys ... ]:[values... ]storage // { key1: value1, key2: value2 ... } // set data to localStorage, recursive creationstorageabcde = 'test' storage // each sync method has a sync copy methodstoragestorage // get data from localStoragestorage // will get object console // the samestorage // the samelocalStorage // the same storage // will get json string // get multiple resultsstorage // will get object { key1: value1, key2: value2 ... } // recursive acquisitionstoragestorageabcde // each sync method has a sync copy methodstorage // remove key from localStoragestorage // none returnstorage // will return test's valuestorage // the same // watchers// active: events triggered by the current pagestorage // passive: events triggered by the other pagestorage // valid eventsconsole // active eventsconsole // passive events // unwatchstoragestorage
Docs:
Some docs here...
const mirror = // create localStorage; createSession is sessionStorageconst storage = mirror_prx // storage proxy
-
### createLocal
create enhanced localStorage
only localstorage supports multi-page data binding##### options: Object
Default options
vueModule: nullstrict: truemirrorOperation: falseupdateMirror: true -
### createSession
create enhanced sessionStorage Since the session of each page is independent, multi-page data binding is not supported
##### options: Object
Default options: same
storage (proxy) object methods
-
### getItem
get item from storage
storage##### key: String, Array
if key is an Array, get multiple, return object. ##### parse: Boolean if parse, try to return JSON.parse(result)
-
### get
Shorthand method name for getItem
storage -
### getAsync
Asynchronous method of getItem
storage -
### setItem
set item from storage
storage##### key: String, Array, Object
If both key and value are array types, set multiple. [...keys] -> [...values]
If key is an Object, set multiple. { key1: value1, key2: value2 } ##### value: Any The value will fix with JSON.stringify -
### set
Shorthand method name for setItem
storage -
### setAsync
Asynchronous method of setItem
storage -
### removeItem
remove item from storage
storage##### key: String, Array
If key is an Array, remove multiple keys. ##### pop: Boolean If pop, removeItem will return the deleted value
-
### remove
Shorthand method name for removeItem
storage -
### removeAsync
Asynchronous method of removeItem
storage -
### clear
clear the storage
storageclear -
### clearAsync
Asynchronous method of clear
storage -
### setChain
Set storage data recursively, try not to overwrite the attributes on the chain.
storage##### keyChain: String
sample: storage.setChain('a.b.c.d.e', 1) == (storage.a.b.c.d.e = 1) ##### value: Any Value will be set on the last object of keyChain
-
### setChainAsync
Asynchronous method of setChain
storage -
### getChain
Get storage data recursively, will get the value of the last object on keyChain.
storage##### key: String
sample: storage.getChain('a.b.c.d.e') == storage.a.b.c.d.e
- ### setChainAsync
Asynchronous method of getChain
storage -
### watch
Add a listener for the specified type of storage change event
storage##### triggerType: String
Must be 'active' or 'passive'.
active: events triggered by the current page
passive: events triggered by the other page ##### eventType: String Add handler function for specified event.
active: must in ["get", "set", "remove", "pop", "clear"]
passive: must in ["set", "remove", "clear"] ##### handler: Function Handler will receives a parameter: event -
### watchActive
Asynchronous method of watch, triggerType is 'active'
storage -
### watchPassive
Asynchronous method of watch, triggerType is 'passive'
storage -
### unwatch
Remove listener for the specified type of storage change event
storage##### triggerType: String
Must be 'active' or 'passive'.
active: events triggered by the current page
passive: events triggered by the other page ##### eventType: String Remove listener for specified event.
active: must in ["get", "set", "remove", "pop", "clear"]
passive: must in ["set", "remove", "clear"] -
### unwatchActive
Asynchronous method of unwatch, triggerType is 'active'
storage -
### watchPassive
Asynchronous method of unwatch, triggerType is 'passive'
storage -
### bindVm
Binding the Vue module (instance) object to ensure that the Vue view can be updated.
storage##### vueModule: Vue instance
Equivalent to "this" in the vue instance.
Please call this function to bind "this" when the page is initialized. -
### toString
return storage data as JSON string
storage -
### _data
return storage data as Object
storage
Storage constructor methods
const _Storage =
-
### create
create and return an enhanced storage mirror object.
_Storage##### type: String
must be 'localStorage' or 'sessionStorage'. ##### options: Object Please see the default options of createLocal
-
### _asyncWrapper
Convert sync functions to async functions and return
_Storage##### func: Function
sync functions
-
### _createObject
Create Object through Array
_Storage)##### list: Array
keys Array ##### defaultValue: any every key's value
-
### _notNull
is val === null || undefined || nan ?
_Storage##### val: any
-
### _parse
Try to parse the JSON string (return the original value if it fails)
_Storage##### value: any
-
### _stringify
Try to stringify the value (return the original value if it fails)
_Storage##### value: any
-
### _zip
Compress two Arrays into an object corresponding to a key value
_Storage##### array1: Array
keys Array ##### array2: Array values Array
Development / test
You are willing to help me improve this project. Or you need to do some testing.
1. Clone this repository
git clone https://github.com/Pure-Peace/the-storages
2. Installation dependencies
npm i
3. Start
npm run dev
4. Open the test page (Default port is 8080)
I provide two sample pages: vue.js 3 and vue.js 2 (CDN unpkg.com)
You can open two pages to experience multi-page data binding and data synchronization.
vue.js 3
http://localhost:8080
vue.js 2
http://localhost:8080/vue2
vue.js 2 中文版页面
http://localhost:8080/vue2_zh
Snowpack
It is recommended to use snowpack to run or build (instead of webpack with babel, beacuse snowpack natively supports es6, and faster)