Debounce Decorator for Vue Class Components
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
-
Error: Decorators are not enabled. You must enable the 'experimentalDecorators' compiler option in your 'tsconfig.json' file.
cause TypeScript compiler option `experimentalDecorators` is not set to `true`.fixAdd or update `"experimentalDecorators": true, "emitDecoratorMetadata": true` in the `compilerOptions` section of your `tsconfig.json`. -
Cannot find module 'vue-class-component' or its corresponding type declarations.
cause The `vue-class-component` package is not installed or incorrectly configured, which is a peer dependency.fixInstall `vue-class-component`: `npm install vue-class-component` or `yarn add vue-class-component`. -
TypeError: Cannot read properties of undefined (reading 'message')
cause Context (`this`) issues when the debounced method is called in a way that loses its `this` binding, especially in older JavaScript environments or incorrect usage.fixEnsure the component method is properly bound to the instance, which `@Component` and decorators typically handle. If passing the method as a callback to an external event listener not managed by Vue, ensure it's explicitly bound (e.g., `this.myMethod.bind(this)`).
Warnings
- breaking This package relies on `vue-class-component`, which is no longer actively maintained and not recommended for new Vue 3 projects. Vue 3 officially deprecates the class component API in favor of the Composition API or Options API.
- gotcha Using decorators requires proper TypeScript configuration (`experimentalDecorators` and `emitDecoratorMetadata` enabled) or a Babel setup that supports legacy decorators. Without this, compilation errors or runtime failures will occur.
- gotcha The `@Debounce` decorator only applies to class methods. It cannot be used directly with functions defined via the Options API or the Composition API in Vue 3. Its scope is strictly limited to methods within classes extending `Vue`.
Install
-
npm install vue-debounce-decorator -
yarn add vue-debounce-decorator -
pnpm add vue-debounce-decorator
Imports
- Debounce
const Debounce = require('vue-debounce-decorator')import { Debounce } from 'vue-debounce-decorator' - @Debounce
const myMethod = @Debounce(300) () => { /* ... */ };class MyComponent extends Vue { @Debounce(300) myMethod() { /* ... */ } }
Quickstart
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>
);
}
}