A local storage and session storage helper script.It can run on WeChat miniprogram and other miniprogram or explorer environment.While using this script you must mixed with MpVUE framework.
It can help you write once and run anywhere if this platform was supported.
This script only recommend wrok with vuex-persistedstate,otherwise you should stringfy your object value
Base on mp-storage
This script support WeChat miniprogram
Alipay program
Baidu program
TouTiao program
HTML
in theory. But it only work well while you mixed this into MpVue
framework.So which platform it will be run is depands on MpVUE
.
If you did not used MpVue
framework,this script actually can not help you anything.
If you are using MpVUE
framework,you may check MpVUE document,it was explained in more detail. Fllows is a brief overview.
npm dev:wx WeChat miniprogram
npm dev:swan Baidu program
npm dev:tt TouTiao program
npm dev:my Alipay program
npm build:wx WeChat miniprogram
npm build:swan Baidu program
npm build:tt TouTiao program
npm build:my Alipay program
Import this script where you wanna use.
import { localStorage, sessionStorage } from '/path-of-multiStorage.js'
localStorage.setItem(key, value)
localStorage.getItem(key)
localStorage.removeItem(key)
localStorage.clear()
localStorage.key(index)
localStorage.length
sessionStorage.setItem(key, value)
sessionStorage.getItem(key)
sessionStorage.removeItem(key)
sessionStorage.clear()
sessionStorage.key(index)
sessionStorage.length
// Recommended
localStorage.setItem('test', '123')
console.log(localStorage.getItem('test')) // 123
// Not recommended
localStorage.test = '123'
console.log(localStorage.test) // 123
This way is not stable enough,recommended set new item with setItem()
import { Store } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { localStorage } from 'mp-storage'
const store = new Store({
// ...
plugins: [
createPersistedState({
storage: localStorage
})
]
})
While it work with
vuex-persistedstate
,you can use localStorage API if you want this only work on local storage, you can also useVuex
to manage status,it can be synchronize to local storage.