npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@johnnywang/vuex-localstorage

v1.0.6

Published

A tiny plugin for Vuex, handling with localStorage

Downloads

1

Readme

Vuex-LocalStore

A Vuex Plugin, help dealing with Vuex data & Web LocalStorage. Since LocalStorage could not automatically interact with some Vue APIs, such as computed, Vuex getters, or v-model.

Install

First Step

$ npm i @johnnywang/vuex-localstorage
or
$ yarn add @johnnywang/vuex-localstorage
/* main.js */
import Vue from 'vue'
import App from './App.vue'
// Vuex store instance
import store from './store'
// import LocalStore
import LocalStore from '@johnnywang/vuex-localstorage'

// register
LocalStore.register(store);

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

Remember to register LocalStore module before new Vue using your store instance.

By registering LocalStore, it will create a Vuex module named LocalStore, it can be accessed by this.$store.LocalStore which we dont recommend to do.

Second Step

Initiate the LocalStore module by calling initLocalStorage in Vuex, this only has to be called once in your App's entrypoint.

/* App.vue */
import { mapActions } from 'vuex'

export default {
  name: 'App',
  methods: {
    ...mapActions('LocalStore', ['initLocalStorage']),
  },
  created() {
    this.initLocalStorage();
  }
}

This method checks user's localStorage, then inject the data into your Vuex.

Usage

Access

After registering, the LocalStore module has been inject into whole Vue instance globally by name $ls by default, which can also set as stateKey in Options.

<!-- Hello.vue -->
<template>
  <div>{{ $ls }}</div>
</template>

<script>
export default {
  name: 'Hello'
}
</script>

In another Vuex module action, we can access it in rootState.LocalStore.$ls.

Programmically change

Globally wherever you want, even in another Vuex module's action, you can use editLocalStore method. It receives an object with key & value, which will change data in Vuex & refresh localStorage automatically.

/* Game.js */
// Another Vuex module

const actions = {
  someHandler({ dispatch }) {
    dispatch('editLocalStore', {
      key: 'name',
      value: 'Kevin Yan',
    }, { root: true });
  }
};
/* Hello.vue */
import { mapActions } from 'vuex';

export default {
  name: 'Hello',
  methods: {
    ...mapActions(['editLocalStore']),
  },
  mounted() {
    this.editLocalStore({
      key: 'name',
      value: 'Gogo brother',
    });
  }
};

Data Binding

Use bindLocalStore method, we can bind LocalStore data into components's computed, then use in v-model

<!-- Hello.vue -->
<template>
  <input type="checkbox" v-model="show" />
</template>

<script>
import { bindLocalStore } from '@johnnywang/vuex-localstorage';

export default {
  name: 'Hello',
  computed: {
    show: bindLocalStore('show'),
  }
};
</script>

This just help you bind the getter/setter of computed with data & editLocalStore.

Be aware that, you don't need to import editLocalStore when using bindLocalStore, even though you still can do it with other uses.

State / Options

state

  • type: {Object}
  • default: {}

By default, state is empty. You can add some default value to localStore in state.

import store from './store';
import LocalStore from '@johnnywang/vuex-localstorage';

// register
LocalStore.register(store, {
  state: {
    name: 'Johnny',
    age: 100,
    show: false
  }
});

getters

  • type: {Object}
  • default: {}

Getters to be set in Vuex module.

stateKey

  • type: {String}
  • default: $ls

The global access key to module state.

userKey

  • type: {String}
  • default: store

real key for Web LocalStorage where stores the data from Vuex.

encode

  • type: {Boolean}
  • default: false

Encode data stores in Web LocalStorage.

Expired Handling

set expire key into state as following:

LocalStore.register(store, {
  state: {
    name: 'Johnny',
    age: 100,
    show: false,
    expire: '2020-03-01' // String will put into new Date()
  }
});

if the user's localstorage data had contain expire key, then if expire date is over now, the data will be injected into Vuex, if the data is outdated, it will be overwriten by Vuex's setting default.

Warning

This plugin is for small use case, and should not be used for storing any sensitive data, we do not responsed for the consequences.

License

MIT

Copyright (c) 2020-present, Johnny Wang