Debounce Decorator for Vue Class Components

1.0.1 · maintenance · verified Tue Apr 21

Vue Debounce Decorator provides an `@Debounce()` decorator to easily apply debouncing to methods within Vue Class Components, enabling rate-limiting of function calls. As of its current stable version 1.0.1, it is designed for use with Vue 2 and the `vue-class-component` library. While effective for its intended environment, this package is tied to a development pattern (Vue Class Components) that has been largely superseded by the Composition API and `<script setup>` in Vue 3. It does not have a rapid release cadence, reflecting the maintenance status of its underlying technologies. Key differentiators include its decorator-based syntax for `vue-class-component` methods, offering a clean, declarative way to add debouncing without manual `setTimeout` management. Alternatives for Vue 3 typically involve using `lodash.debounce` directly within the Composition API or dedicated debounce directives for input elements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates applying the `@Debounce` decorator to a Vue class component method to rate-limit input handling.

import Vue from 'vue';
import Component from 'vue-class-component';
import { Debounce } from 'vue-debounce-decorator';

@Component
export default class App extends Vue {
  message: string = '';

  @Debounce(500)
  handleInput(event: Event) {
    const target = event.target as HTMLInputElement;
    this.message = target.value;
    console.log(`Debounced input: ${this.message}`);
  }

  mounted() {
    // Simulate user typing after 100ms, then 200ms, then 600ms
    setTimeout(() => { this.handleInput({ target: { value: 'H' } } as unknown as Event); }, 100);
    setTimeout(() => { this.handleInput({ target: { value: 'He' } } as unknown as Event); }, 200);
    setTimeout(() => { this.handleInput({ target: { value: 'Hello' } } as unknown as Event); }, 600);
  }

  render() {
    return (
      <div>
        <h1>Vue Debounce Decorator Example</h1>
        <input type="text" onInput={this.handleInput} placeholder="Type something..." />
        <p>Current (debounced) message: {this.message}</p>
      </div>
    );
  }
}

view raw JSON →