Code Monkey home page Code Monkey logo

mp-storage's Introduction

mp-storage

GitHub followers

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

Platform

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.

How to chioce a platform

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.

Chioce a platform on develope environment

npm dev:wx WeChat miniprogram

npm dev:swan Baidu program

npm dev:tt TouTiao program

npm dev:my Alipay program

Chioce a platform on production environment

npm build:wx WeChat miniprogram

npm build:swan Baidu program

npm build:tt TouTiao program

npm build:my Alipay program

Quickstart

Import this script where you wanna use.

import { localStorage, sessionStorage } from '/path-of-multiStorage.js'

API

localStorage

localStorage.setItem(key, value)

localStorage.getItem(key)

localStorage.removeItem(key)

localStorage.clear()

localStorage.key(index)

localStorage.length

sessionStorage

sessionStorage.setItem(key, value)

sessionStorage.getItem(key)

sessionStorage.removeItem(key)

sessionStorage.clear()

sessionStorage.key(index)

sessionStorage.length

Demo

Just use this

// 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()

Work with vuex-persistedstate

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 use Vuex to manage status,it can be synchronize to local storage.

mp-storage's People

Contributors

nicemorning007 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.