Vue IMask Plugin
vue-imask is the official Vue.js plugin for IMask.js, a robust and versatile JavaScript input mask library. It provides native Vue 2 and Vue 3 support through a component (`<imask-input>`), a directive (`v-imask`), and a composable (`useIMask`) for granular control over input formatting. The package is currently at version 7.6.1 and receives frequent updates, typically on a monthly basis, incorporating new mask features and bug fixes from the core IMask.js library, along with Vue-specific enhancements. Its key differentiators include comprehensive mask types (pattern, number, date, enum, dynamic, etc.), excellent IME support, and a flexible API that allows for both declarative (component/directive) and programmatic (composable) masking, making it suitable for a wide range of input formatting requirements in Vue applications. It ships with full TypeScript type definitions, ensuring type safety and improved developer experience.
Common errors
-
Failed to resolve import "vue-imask" from "src/App.vue". Is the file system or an NPM dependency missing?
cause The `vue-imask` package is not installed or incorrectly added to `package.json`.fixRun `npm install vue-imask` or `yarn add vue-imask` to install the package. -
Error: Cannot find module '@vue/composition-api'
cause You are using `vue-imask` (or its composable/component features) in a Vue 2 project without the `@vue/composition-api` plugin installed.fixInstall the necessary plugin: `npm install --save-dev @vue/composition-api`. -
Property 'value' does not exist on type 'Event'.
cause When using the `v-imask` directive's `@accept` or `@complete` events, the event detail object is accessed incorrectly in TypeScript.fixAccess the mask reference via `e.detail` as `const maskRef = (e as CustomEvent).detail; this.value = maskRef.value;`. -
Component is missing template or render function.
cause When using `IMaskComponent` in a Vue 2 single-file component, it needs to be properly registered in the `components` option.fixEnsure `components: { 'imask-input': IMaskComponent }` is present in your component's options, and use it in the template as `<imask-input ... />`.
Warnings
- breaking The behavior of `v-model` and the `accept` events changed significantly between Vue 2 and Vue 3. In Vue 3, you can use `v-model:value`, `v-model:masked`, `v-model:unmasked`, or `v-model:typed`. The `onAccept` callback in Vue 2 depends on the `unmask` prop, while Vue 3 introduces distinct `onAccept:masked`, `onAccept:unmasked`, and `onAccept:typed` events.
- gotcha For Vue 2 applications, especially when using the composable (`useIMask`) or the component with advanced features, the `@vue/composition-api` plugin is required. For Vue 2 Nuxt.js projects, `@nuxtjs/composition-api` is also needed.
- gotcha `vue-imask` (and the core `imask` library) adopted `type: "module"` across all packages (except Angular) since v7.1.1, making it ESM-first. While a CJS build was added in v7.2.0, ESM imports are generally preferred and more straightforward.
- gotcha Since v7.1.2, the `esm` part in import paths (e.g., `import 'imask/esm/masked/number'`) can be skipped, simplifying imports to `import 'imask/masked/number';` for core IMask.js modules.
- gotcha The `v-imask` directive might not consistently update `v-model` bindings in all scenarios, especially when complex mask configurations or dynamic changes are involved.
Install
-
npm install vue-imask -
yarn add vue-imask -
pnpm add vue-imask
Imports
- IMaskComponent
const IMaskComponent = require('vue-imask');import { IMaskComponent } from 'vue-imask'; - IMaskDirective
import IMaskDirective from 'vue-imask/directive';
import { IMaskDirective } from 'vue-imask'; - useIMask
import { useIMask } from 'vue-imask/dist/vue-imask.esm.js';import { useIMask } from 'vue-imask';
Quickstart
<template>
<imask-input
v-model:typed="numberModel"
:mask="Number"
radix="."
@accept:masked="onAcceptMasked"
@accept:unmasked="onAcceptUnmasked"
placeholder='Enter number here'
/>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
import { IMaskComponent } from 'vue-imask';
// Also useful: Number, Pattern, Date, Enum masks from imask core
import { NumberMask } from 'imask';
export default defineComponent({
components: {
'imask-input': IMaskComponent
},
setup() {
const numberModel = ref<number | string>('');
const onAcceptMasked = (value: string) => {
console.log('Masked value:', value);
};
const onAcceptUnmasked = (unmaskedValue: string) => {
console.log('Unmasked value:', unmaskedValue);
};
return {
numberModel,
Number: NumberMask, // Or simply 'Number' if importing from 'imask' core
onAcceptMasked,
onAcceptUnmasked,
};
}
});
</script>