Vue Debounce Directive
vue-debounce is a Vue.js directive that provides a straightforward way to debounce events, primarily user input, within Vue 3 applications. As of its current stable version, 5.0.1, it exclusively supports Vue 3; users requiring Vue 2 compatibility are directed to the separate `vue2-debounce` package or to remain on v4 of vue-debounce. The library is actively maintained, with a cadence of minor and patch releases, and major versions introducing significant architectural shifts such as the transition to ES modules. Its key differentiators include a simple declarative usage model via the `v-debounce` directive, support for various event listeners, customizable debounce times, and several modifiers like `lock`, `fireonempty`, and `trim` for fine-grained control over debouncing behavior. It supports both global application-level registration and component-level usage.
Common errors
-
TypeError: (0 , vue_debounce__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause Attempting to import `vue-debounce` (v5+) in a CommonJS context or with a bundler configuration that does not correctly resolve ES modules.fixEnsure your project uses ES module `import` syntax (`import vueDebounce from 'vue-debounce'`) and that your build environment (e.g., Webpack, Vite, Rollup) is configured to handle ES modules properly. -
[Vue warn]: Failed to resolve directive: debounce
cause The `v-debounce` directive has not been correctly registered with your Vue application, either globally or locally within the component.fixGlobally: `app.directive('debounce', vueDebounce(...))` in your `main.js`/`main.ts`. Locally: `directives: { debounce: vueDebounce(...) }` in your component options, or within `<script setup>` in Vue 3 via `const vDebounce = vueDebounce(...)`. -
Cannot read properties of undefined (reading 'install')
cause Attempting to use `vue-debounce` v5+ (which is Vue 3 only) with a Vue 2 application.fixMigrate to `vue2-debounce` for Vue 2 projects, or downgrade `vue-debounce` to v4.
Warnings
- breaking Vue Debounce v5.0.0 completely removes support for Vue 2. If your project relies on Vue 2, you must either stay on `vue-debounce` v4 or migrate to the dedicated `vue2-debounce` package.
- breaking As of v5.0.0, `vue-debounce` is published as an ES module (`type: 'module'` in package.json). This changes how it should be imported, particularly in CommonJS environments or older build setups.
- breaking Vue Debounce v4.0.0 removed the `getDirective` flow, simplifying usage by separating Vue 2 and Vue 3 imports and direct directive registration. Attempts to use `getDirective` will fail.
- gotcha The `trim` option/modifier, introduced in v3.0.0, only affects the internal logic for determining if an input should fire (e.g., preventing empty string callbacks). The value passed to your debounced function will *not* be automatically trimmed.
- gotcha The behavior of `fireonempty` was made stricter in v3.0.0 to fix a bug. If you relied on previous looser behavior, your debounced function might not fire when an input is emptied.
Install
-
npm install vue-debounce -
yarn add vue-debounce -
pnpm add vue-debounce
Imports
- vueDebounce
const vueDebounce = require('vue-debounce')import vueDebounce from 'vue-debounce'
- DebounceOptions
import type { DebounceOptions } from 'vue-debounce' - Vue 2 support
import vueDebounce from 'vue-debounce'
import vueDebounce from 'vue2-debounce'
Quickstart
import vueDebounce from 'vue-debounce';
import { createApp } from 'vue';
import App from './App.vue';
const app = createApp(App);
// Register the directive globally with custom options
app
.directive('debounce', vueDebounce({
lock: false, // Lock all debounced inputs by default
defaultTime: '500ms', // Default debounce time for directives without a specified time
listenTo: ['keyup', 'change'] // Listen to multiple events
}))
.mount('#app');
// In your App.vue template:
// <template>
// <div>
// <input v-debounce:800ms.trim="mySearchFunction" placeholder="Search..." />
// <button v-debounce="myClickHandler">Click Me</button>
// </div>
// </template>
// In your component script:
// export default {
// methods: {
// mySearchFunction(value) {
// console.log('Debounced search for:', value);
// },
// myClickHandler() {
// console.log('Debounced click!');
// }
// }
// }