vuejs-storage
TypeScript icon, indicating that this package has built-in type declarations

3.1.1 • Public • Published

vuejs-storage

Vue.js and Vuex plugin to persistence data with localStorage/sessionStorage

npm npm GitHub Workflow Status codecov

Purpose

This plugin provide a simple binding with localStorage and sessionStorage (or something similar) to Vue and Vuex.

It has no dependencies, so it is really small.

  • .js size: 5.75KB (1.7KB gzipped)
  • .min.js size: 2.21KB (1.1KB gzipped)

Usage

//in webpack environment:
import vuejsStorage from 'vuejs-storage'
//in browser script tag:
const vuejsStorage = window.vuejsStorage

Vue.use(vuejsStorage)

//vue example
new Vue({
  //...
  data: {
    count: 0,
    text: ''
  },
  storage: {
    keys: ['count'],
    //keep data.count in localStorage
    namespace: 'my-namespace'
  }
})

//vuex example
const store = new Vuex.Store({
  //state...
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  },
  plugins: [
    vuejsStorage({
      keys: ['count'],
      //keep state.count in localStorage
      namespace: 'my-namespace',
      driver: vuejsStorage.drivers.sessionStorage
      //if you want to use sessionStorage instead of localStorage
    })
  ]
})

Nested key

data: {
  a: {
    b: 1,
    c: 2
  }
},
storage: {
  namespace: 'test',
  keys: ['a.b']
  //only keep a.b in localStorage
}

Vuex modules

state: {
  a: 1
},
modules: {
  moduleA: {
    state: {
      a: 2
    }
  }
},
plugins: [
  vuejsStorage({
    namespace: 'test',
    keys: ['moduleA','a']
    // keep both root's state.a & moduleA's state
  })
]

Multiple storage

data: {
  a: 1,
  b: 2
},
storage: [
  {
    namespace: 'test',
    keys: ['a']
  },
  {
    namespace: 'test',
    keys: ['b'],
    driver: vuejsStorage.drivers.sessionStorage
  }
]

API

vuejsStorage

Vue plugin

Vue.use(vuejsStorage)

vuejsStorage(option)

Create a Vuex plugin

const vuexplugin = vuejsStorage(/* option object*/)

option

Option object, can be used when create Vuex plugin or in Vue option storage field

{
  keys: [], //array of string
  /*
  this option is different when use in vue and vuex
  when used in Vue constructor option, keys means which data should be keep in localStorage
  when used in Vuex plugin, keys mean which state should be keep in localStorage
  */
  driver: vuejsStorage.drivers.sessionStorage, //any object has 'set','get','has' api, default: vuejsStorage.drivers.localStorage
  namespace: 'ns', //a string, REQUIRED
  merge: _assign //a function to merge object like Object.assign, default: internal implementation(src/assign.ts)
}

Examples

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.1.160latest

Version History

VersionDownloads (Last 7 Days)Published
3.1.160
3.1.048
3.0.51
3.0.40
3.0.32
3.0.21
3.0.15
3.0.00
2.4.01
2.3.31
2.3.20
2.3.10
2.3.00
2.2.60
2.2.51
2.2.40
2.2.30
2.2.20
2.2.10
2.2.00
2.1.10
2.1.00
2.0.00
1.1.00
1.0.30
1.0.10
1.0.00
0.9.10
0.9.00
0.8.30
0.8.20
0.8.10
0.8.00
0.7.10
0.7.00
0.6.10
0.6.00
0.5.00
0.4.10
0.4.00
0.3.10
0.3.00
0.2.00
0.1.20
0.1.10
0.1.00
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i vuejs-storage

Weekly Downloads

120

Version

3.1.1

License

MIT

Unpacked Size

815 kB

Total Files

63

Last publish

Collaborators

  • maple3142