Vue Country Code Selector

1.1.3 · maintenance · verified Sun Apr 19

vue-country-code is a Vue 2 component designed for selecting country codes, typically integrated into forms for phone number inputs. It provides a customizable dropdown list of countries, their flags, and international dial codes. Developers can configure default selections, specify preferred or excluded countries, and enable/disable features like flag display or automatic country detection based on the user's IP address. Currently at version 1.1.3, the package appears to be in a maintenance state, leveraging established external libraries such as libphonenumber-js for robust telephone number parsing and validation, and ipinfo.io for initial country detection. Its core functionality is based on a fork of vue-tel-input, offering a stable but not actively feature-developing solution primarily for Vue 2 applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to globally register and use the `vue-country-code` component within a Vue 2 application, including handling the `onSelect` event to capture country details and displaying them.

import Vue from 'vue';
import VueCountryCode from 'vue-country-code';

// Register the component globally for easy use across your app
Vue.use(VueCountryCode);

// Define a root Vue instance to demonstrate the component
new Vue({
  el: '#app',
  template: `
    <div id="app">
      <h1>Select Your Country Code</h1>
      <p>This component allows users to pick a country, displaying its flag and dial code.</p>
      <vue-country-code
        @onSelect="handleCountrySelect"
        :preferredCountries="['us', 'gb', 'de']"
        :defaultCountry="'us'"
        :enabledFlags="true"
        :dropdownOptions="{ disabledDialCode: false }"
      ></vue-country-code>
      <div v-if="selectedCountry" style="margin-top: 20px;">
        <h2>Selected Country Details:</h2>
        <p><strong>Name:</strong> {{ selectedCountry.name }}</p>
        <p><strong>ISO2:</strong> {{ selectedCountry.iso2 }}</p>
        <p><strong>Dial Code:</strong> +{{ selectedCountry.dialCode }}</p>
      </div>
    </div>
  `,
  data() {
    return {
      selectedCountry: null
    };
  },
  methods: {
    handleCountrySelect({ name, iso2, dialCode }) {
      this.selectedCountry = { name, iso2, dialCode };
      console.log('Country Selected:', { name, iso2, dialCode });
    }
  }
});

// To run this code, ensure you have an HTML file with `<div id="app"></div>` and Vue 2 loaded.

view raw JSON →