Vue.js Typeahead/Autocomplete for Bootstrap 4
Vue Typeahead Bootstrap is an autocomplete component for Vue 2 applications, specifically designed to integrate with Bootstrap 4's styling and utility classes. It provides a `list-group` based interface for suggestions, supporting features like custom suggestion slots and event emission for focus/blur. The project is currently at version 2.12.0, with recent releases indicating ongoing maintenance and minor feature enhancements (e.g., v2.13.0 recently). It serves as a spiritual successor to the `vue-bootstrap-typeahead` project, which lost its maintainer, ensuring continued support for users requiring a Bootstrap 4 compatible typeahead in their Vue 2 applications. Its primary differentiators are its strong coupling with Bootstrap 4 and its dedicated support for Vue 2, making it a suitable choice for legacy projects or those specifically constrained to these versions.
Common errors
-
Error in render: 'component' is not defined
cause The component was not properly registered or imported as a default export.fixEnsure `import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';` is used and the component is registered either globally via `Vue.component` or locally within the `components` option of a Vue component. -
TypeError: Cannot read properties of undefined (reading 'extend')
cause Commonly occurs when Vue is not correctly bundled or accessible, or when the component attempts to use Vue functionality before Vue is properly initialized. Could also indicate a CJS/ESM mismatch in a build environment.fixVerify your build configuration ensures Vue is correctly resolved and that `vue-typeahead-bootstrap` is imported as an ES module. If using CommonJS, ensure transpilation handles the default export correctly.
Warnings
- breaking Migration from versions prior to 2.0 requires renaming references. The package name changed from `vue-bootstrap-typeahead` to `vue-typeahead-bootstrap`, and the component name changed from `VueBootstrapTypeahead` to `VueTypeaheadBootstrap`.
- gotcha This component is built for Vue 2 and Bootstrap 4. It is not directly compatible with Vue 3 or Bootstrap 5 without significant adaptation or a different component library.
- gotcha The component is a default export, so destructuring imports (e.g., `import { VueTypeaheadBootstrap } from '...'`) will fail, resulting in `undefined`.
Install
-
npm install vue-typeahead-bootstrap -
yarn add vue-typeahead-bootstrap -
pnpm add vue-typeahead-bootstrap
Imports
- VueTypeaheadBootstrap
const VueTypeaheadBootstrap = require('vue-typeahead-bootstrap');import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';
- Component Registration
Vue.use(VueTypeaheadBootstrap);
import Vue from 'vue'; import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap'; Vue.component('vue-typeahead-bootstrap', VueTypeaheadBootstrap); - Local Component Import
import { VueTypeaheadBootstrap } from 'vue-typeahead-bootstrap';import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap'; export default { components: { VueTypeaheadBootstrap }, // ... }
Quickstart
import Vue from 'vue';
import App from './App.vue';
import 'bootstrap/dist/css/bootstrap.min.css'; // Ensure Bootstrap CSS is loaded
import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';
Vue.component('vue-typeahead-bootstrap', VueTypeaheadBootstrap);
new Vue({
el: '#app',
template: `
<div id="app" class="container mt-5">
<h1>Search Countries</h1>
<vue-typeahead-bootstrap
v-model="selectedCountry"
:data="countries"
placeholder="Type a country..."
@hit="countrySelected"
size="lg"
></vue-typeahead-bootstrap>
<p v-if="selectedCountry" class="mt-3">Selected: {{ selectedCountry }}</p>
</div>
`,
data() {
return {
selectedCountry: null,
countries: [
'United States', 'Canada', 'Mexico', 'Brazil', 'Argentina',
'United Kingdom', 'France', 'Germany', 'Italy', 'Spain',
'China', 'India', 'Japan', 'Australia', 'South Africa'
]
};
},
methods: {
countrySelected(item) {
console.log('Selected item:', item);
}
}
});