Vue Select2 Component
The `v-select2-component` package, currently at version 0.4.7, provides a Vue 2 wrapper for the popular jQuery Select2 plugin. It enables developers to integrate enhanced `<select>` elements with search, tagging, and remote data capabilities into their Vue 2 applications. The library directly leverages jQuery and the Select2 library, meaning these are required dependencies and must be loaded in the application. As indicated by its reference to Vue CLI v2.9.1 and a pre-1.0 version number, the project appears to be in a maintenance state, primarily supporting Vue 2 applications, with no active development for Vue 3 or modern front-end tooling. Its primary differentiator is its direct, unopinionated wrapper approach for Select2 within the Vue 2 ecosystem.
Common errors
-
TypeError: $(...).select2 is not a function
cause The Select2 jQuery plugin has not been loaded or correctly attached to the jQuery object before the Vue component attempts to initialize it.fixVerify that `jQuery` is loaded, followed by the `select2` JavaScript file. Ensure they are available in the global scope or correctly imported and bundled by your build system. -
Uncaught ReferenceError: jQuery is not defined
cause The jQuery library itself is not loaded or accessible in the execution environment where the component or Select2 plugin attempts to run.fixMake sure the `jquery` package is installed (`npm install jquery`) and imported/included as the very first script in your application, before `select2` or `v-select2-component`. -
Failed to mount component: template or render function not defined
cause This general Vue 2 error can occur if the `v-select2-component` component is not correctly registered in your Vue application (globally or locally), or if your Vue build isn't configured to handle SFCs correctly.fixEnsure `v-select2-component` is properly imported and declared in the `components` option of its parent component, or globally registered using `Vue.component('Select2', Select2)`.
Warnings
- breaking This component is built exclusively for Vue 2 and is fundamentally incompatible with Vue 3 due to significant architectural changes in Vue's component API, reactivity system, and lifecycle hooks.
- gotcha This component has a hard dependency on both `jQuery` and the `select2` library. These must be explicitly installed and loaded into your application (e.g., globally or via imports) before `v-select2-component` is initialized. Failure to do so will result in runtime errors.
- gotcha The project appears to be in a maintenance-only state, referencing older tooling (Vue CLI v2) and having a pre-1.0 version (0.4.7). This suggests limited or no active development for new features, compatibility with modern JavaScript ecosystems, or security updates beyond basic functionality for Vue 2.
Install
-
npm install v-select2-component -
yarn add v-select2-component -
pnpm add v-select2-component
Imports
- Select2
const Select2 = require('v-select2-component');import Select2 from 'v-select2-component';
- Vue.component
Vue.component('Select2', require('v-select2-component'));import Select2 from 'v-select2-component'; Vue.component('Select2', Select2); - Local Component
import Select2 from 'v-select2-component'; export default { components: { Select2 } };
Quickstart
<template>
<div id="app">
<h3>v-select2-component Quickstart</h3>
<Select2 v-model="myValue" :options="myOptions" :settings="select2Settings" @change="myChangeEvent" @select="mySelectEvent" />
<p>Selected Value: <strong>{{ myValue }}</strong></p>
<button @click="myValue = 'op2'">Set to 'op2'</button>
<button @click="myValue = { id: 'op4', text: 'Option 4' }">Set to 'Option 4' (object)</button>
<hr />
<p>Multiple Select Example:</p>
<Select2 v-model="multiValue" :options="multiOptions" :settings="{ multiple: true, placeholder: 'Select multiple options' }" @change="multiChangeEvent" />
<p>Multiple Selected Values: <strong>{{ multiValue }}</strong></p>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import Select2 from 'v-select2-component';
// IMPORTANT: Ensure jQuery and Select2 CSS/JS are loaded globally or via your build process.
// For example, in your main.ts or main.js:
// import 'jquery';
// import 'select2';
// import 'select2/dist/css/select2.min.css';
interface Select2Option {
id: string | number;
text: string;
}
export default Vue.extend({
name: 'App',
components: {
Select2
},
data() {
return {
myValue: 'op1' as string | Select2Option,
myOptions: ['op1', 'op2', 'op3', { id: 'op4', text: 'Option 4' }] as Array<string | Select2Option>,
select2Settings: {
dropdownParent: 'body',
width: '100%',
minimumResultsForSearch: 5
},
multiValue: ['itemA'] as Array<string | number>,
multiOptions: [
{ id: 'itemA', text: 'Item A' },
{ id: 'itemB', text: 'Item B' },
{ id: 'itemC', text: 'Item C' }
] as Array<Select2Option>
};
},
methods: {
myChangeEvent(val: string | Select2Option | Array<string | Select2Option>): void {
console.log('Single select change event:', val);
},
mySelectEvent(option: { id: string | number; text: string }): void {
console.log('Single select option selected:', option);
},
multiChangeEvent(val: Array<string | Select2Option>): void {
console.log('Multiple select change event:', val);
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>