New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

vue-cache-decorator

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-cache-decorator

caching the data of your vue component made easy

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

What is this?

a typescript decorator that can be used to cache the state of your vue component properties. the decorator works alongside with vue-class-component and vue2

This can be useful for example to cache a filter property so that when the user leaves the component and comes back the filter is still applied.

// table.vue
<template>
    .....
</template>
<script lang="ts">
import Cache from 'vue-cache-decorator'
import Component from "vue-class-component";
import Vue from "vue";

@Component({})
export default class Table extends Vue {
    @Cache()
    filter = {
        categoryId: 0,
        page: 0,
        limit: 10,
        sort: 'createdAt',
    }
}
</script>

whenever the user changes the filter, the new value is cached. the next time the user comes back to the component, the old filter value is retrieved from the localStorage.

Install

npm install vue-cache-decorator
yarn add vue-cache-decorator

How does it work?

the decorator will create a watcher on the property and when the value changes, the new value is cached in the storage.
the decorator will also set the old value of the property when the component is created. the last example is equivalent to the following:

// table.vue
import Cache from 'vue-cache-decorator'
import Component from "vue-class-component";
import Vue from "vue";

@Component({})
export default class Table extends Vue {
    filter = {}
    
    created() {
        this.filter = JSON.parse(localStorage.getItem('Table-[filter]'))
        this.$watch('filter', {
            handler: (newValue) => {
                localStorage.setItem('Table-[filter]', JSON.stringify(newValue))
            },
            deep: true,
        })
    }
}

Options

the decorator function can accept an options object. the following options are available:

OptionDescriptionTypeDefault
cacheKeythe storage key for the property when storing/retrievingstringgenerated value ${componentName}-[${propertyName}]
deepvue deep watchbooleantrue
cacheStorethe storage interface to useStoragewindow.localStorage
exceptproperty name(s) to ignorestring|string[]undefined
onlyproperty name(s) only to be cached, mutually exclusive with exceptstring|string[]undefined
globalDispatchfire a global window event on value change. newValue will be stored in event.detail property (on CustomEvent type). event name will be value of this propertystringundefined

the decorator accept one type parameter. this can be used for auto-completion and auto-refactoring for property names in only and except options.

FAQs

Package last updated on 29 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts