Code Monkey home page Code Monkey logo

vue-persist's Introduction

vue-persist

NPM version NPM downloads Build Status donate

Persist data in localStorage or anywhere for Vue.js apps

Install

yarn add vue-persist

Usage

import VuePersist from 'vue-persist'

Vue.use(VuePersist)

new Vue({
  template: '<input v-model="name" />',

  data: { name: '' },

  persist: ['name']
})

Then the data of name will be stored at localStorage and kept in sync.

Check out the demo, just type some words and refresh the page.

You can also manually call this.$persist(['some-data']) instead of using the component option.

API

Vue.use(VuePersist, [options])

options

name

Type: string
Default: persist:store

The name of the localStorage store.

expiration

Type: number
Default: 0

Expire store in a specific amount of time (in millisecond), default to never expire.

read

Type: function
Default: k => localStorage.get(k)

The function we use to get stored data.

write

Type: function
Default: (k, v) => localStorage.set(k, v)

The function we use to store data.

clear

Type: function
Default: k => localStorage.removeItem(k)

The function we use to clear data in store.

this.$persist(keys[, name, expiration])

keys

Type: Array
Required: true

Array of keys of state you want to keep in sync with localStorage.

name

Type: string
Default: options.name

expiration

Type: number
Default: options.expiration

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

vue-persist © egoist, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).

egoistian.com · GitHub @egoist · Twitter @rem_rin_rin

vue-persist's People

Contributors

biigpongsatorn avatar egoist avatar skrichten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vue-persist's Issues

Deep watch

Would be nice if there was an option for deep watch when you want to persist an object. Or maybe it should use deep watch automatically if the persisted value is an object?

How can I use this plugin from multiple Vue components

If I have a webpage with more than one Vue component, say I have two Vue components on the page doing completely different things and both components wants to persist some data. How do I do this? When I try it the data from the first component is overwritten by data from the second component.

Feature suggestion: exposing functions

How would you feel about exposing the basic read/write functions? Something like this...

Vue.storage = {
    write,
    writeMulti: items => items.forEach(data => write(data.key, data.value)),
    read,
    readMulti: keys => { return keys.map(key => read(key)) },
    clear,
    clearMulti: keys => keys.forEach(key => clear(key))
  }

  Object.defineProperties(Vue.prototype, {
    $storage: {
      get() {
        return Vue.storage;
      }
    }
  })

多组件并发使用时一部分组件写入丢失

环境:iOS Safari(macOS 无法复现)

使用 Vue+Webpack 开发,全局引入 Persist 后,在各组件中直接定义不重复的 persist,实现所有组件都能持久化数据。按上述方式使用时发现,多组件并发更新时,后更新的组件持久化数据将会覆盖先更新的组件的更改。

可能是由于不同组件都独立维护了 store 对象,导致组件间的 store 不同步,从而写入时出现覆盖。(index.js#L40

Component option API

Have you considered accepting a component option named persist?
I've seen this pattern in other plugins.
It would make the usage more simple:

import VuePersist from 'vue-persist'

Vue.use(VuePersist)

new Vue({
  template: '<input v-model="name" />',
  data: { name: '' },
  persist: ['name']
})

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.