Vue Tel Input
Vue Tel Input is a Vue.js component that provides an international telephone input field, integrating country code selection, formatting, and validation. It leverages `libphonenumber-js` for robust and accurate parsing, formatting, and validation of phone numbers according to international standards. The current stable version is `9.8.0`, with regular minor and patch releases addressing bugs, improving features like TypeScript support, accessibility, and updating underlying dependencies. Key differentiators include its dedicated Vue 3 support (with a separate legacy branch for Vue 2), flexible installation options (global plugin, local component, or lazy loading for performance), and comprehensive customization capabilities for styling and behavior. It offers a standardized solution for handling diverse international phone number inputs in web applications.
Common errors
-
Cannot find module 'libphonenumber-js' or its corresponding type declarations.
cause The `libphonenumber-js` package, a peer dependency, is not installed in your project.fix`npm install libphonenumber-js` -
Property 'VueTelInput' does not exist on type 'typeof import("vue-tel-input")'.cause Attempting to use `app.use({ VueTelInput })` or a named import when `app.use()` expects the default export for global registration.fixUse the default import for global registration: `import VueTelInput from 'vue-tel-input'; app.use(VueTelInput);` -
Module not found: Error: Can't resolve 'vue-tel-input/dist/vue-tel-input.css'
cause An incorrect or deprecated path is being used to import the component's CSS styles.fixUpdate your CSS import statement to: `import 'vue-tel-input/vue-tel-input.css';` -
TypeError: Cannot read properties of undefined (reading 'install') when using `app.use(window['vue-tel-input'])` in browser.
cause The `vue-tel-input` script loaded via CDN did not automatically install or `window['vue-tel-input']` is not the correct reference to the plugin's install function.fixEnsure Vue is loaded before `vue-tel-input` for automatic installation. If installing manually, verify the global variable name and that the plugin object has an `install` method.
Warnings
- breaking Starting from `v9.3.0`, `libphonenumber-js` and `vue` were moved to peer dependencies. They must now be explicitly installed in your project.
- gotcha TypeScript declaration files were historically incorrect or missing in versions prior to `v9.8.0`, leading to potential type errors or incomplete IntelliSense.
- gotcha The CSS import path was fixed in `v9.2.0`. Older versions might have used a different or incorrect path, leading to styles not being applied.
- gotcha When implementing component lazy loading, it's crucial to load both the JavaScript component and its CSS asynchronously and correctly, typically using `Promise.all` with bundlers like Webpack/Rollup. Importing CSS via Vue SFC `<style>` tags in lazy-loaded components can cause the CSS to be bundled with the main vendor chunk instead of being lazy-loaded.
Install
-
npm install vue-tel-input -
yarn add vue-tel-input -
pnpm add vue-tel-input
Imports
- VueTelInput (global)
import { VueTelInput } from 'vue-tel-input'; app.use(VueTelInput);import VueTelInput from 'vue-tel-input'; app.use(VueTelInput);
- VueTelInput (component)
import VueTelInput from 'vue-tel-input';
import { VueTelInput } from 'vue-tel-input'; - CSS Styles
import 'vue-tel-input/dist/vue-tel-input.css';
import 'vue-tel-input/vue-tel-input.css';
Quickstart
<template>
<vue-tel-input v-model="phone" mode="international"></vue-tel-input>
</template>
<script lang="ts">
import { ref, defineComponent } from 'vue';
import { VueTelInput } from 'vue-tel-input';
import 'vue-tel-input/vue-tel-input.css';
export default defineComponent({
components: {
VueTelInput,
},
setup() {
const phone = ref<string | null>(null);
return {
phone,
};
},
});
</script>