Vue Country Flag Component
vue-country-flag-next is a dedicated Vue 3 component designed for displaying country flags, primarily identified by ISO 3166-1 alpha-2 or alpha-3 codes. The current stable version is 2.3.2, with an active release cadence addressing bug fixes and minor improvements. A key differentiator is its optimized footprint, achieved in v2.1.0 by loading flag images as separate files rather than base64 encoded strings within the CSS, reducing the package size by approximately 700kb. It natively supports TypeScript and integrates seamlessly into Vue 3 projects, offering optional properties for size, rounded borders, and shadows.
Common errors
-
Failed to resolve component: country-flag
cause The `CountryFlag` component has not been correctly registered globally or locally within the Vue application.fixEnsure you have either registered the component globally using `app.component('country-flag', CountryFlag)` (for `main.js`) or locally within a component's `components` option: `components: { CountryFlag }`. -
TypeError: Cannot read properties of undefined (reading 'install')
cause This error can occur if you try to use `CountryFlag` as a Vue plugin (e.g., `app.use(CountryFlag)`), but it is a component, not a plugin with an `install` method.fixImport `CountryFlag` as a component and register it either globally with `app.component()` or locally within another component's `components` option, instead of trying to use `app.use()`.
Warnings
- breaking Prior to v2.1.0, flag images were encoded as Base64 strings directly in the component's CSS. From v2.1.0 onwards, images are loaded as separate files, significantly reducing package footprint but potentially requiring server configuration to serve these assets correctly.
- breaking In v2.0.0, the `.vue` file was removed from the final npm bundle. This change primarily affects consumers who might have been directly importing or processing the `.vue` single-file component source.
- gotcha Early versions (prior to v2.0.1) had an issue where the Andorra flag might not be visible due to interactions with AdBlock software, likely due to CSS class names or image URLs resembling ad patterns.
- gotcha The component requires Vue 3.x as a peer dependency. Attempting to use it with Vue 2.x will result in compatibility errors due to breaking changes in Vue's API between major versions.
Install
-
npm install vue-country-flag-next -
yarn add vue-country-flag-next -
pnpm add vue-country-flag-next
Imports
- CountryFlag
const CountryFlag = require('vue-country-flag-next');import CountryFlag from 'vue-country-flag-next';
- App.component
import { CountryFlag } from 'vue-country-flag-next';import CountryFlag from 'vue-country-flag-next'; app.component('country-flag', CountryFlag);
Quickstart
npm install vue-country-flag-next
// main.js or similar entry file
import { createApp } from 'vue';
import App from './App.vue';
import CountryFlag from 'vue-country-flag-next';
const app = createApp(App);
// Register globally for easy use across your application
app.component('country-flag', CountryFlag);
app.mount('#app');
// In a Vue component's template (e.g., App.vue):
// <template>
// <div>
// <country-flag country='it' size='big'/>
// <country-flag country='hr' size='normal'/>
// <country-flag country='fr' size='small'/>
// <country-flag country='rus'/>
// <country-flag country='de' rounded/>
// <country-flag country='jp' shadow/>
// </div>
// </template>