How to call an event on selecting of autocomplete option using the vue-typeahead-bootstrap package in VueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

To use the autocomplete functionality in our app. We are using the vue-typeahead-bootstrap package in BootstrapVue.

Today I am going to show you How you autocomplete using the vue-typeahead-bootstrap package in VueJS?

Table of contains

  • Setup the Vue.js
  • Install the Bootstrap and BootstrapVue
  • Create FirstComponent.vue and import it into App.js
  • Install the vue-typeahead-bootstrap package
  • Add autocomplete component to your app

Let’s start today’s tutorial How do I autocomplete using the vue-typeahead-bootstrap package in VueJS?

Setup the Vue.js

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, which will show you step by step process of installation.

How to Install the VueJS project?

Install the Bootstrap and BootstrapVue

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, which will show you step by step process of installation. In that article, I had also show you how to install BootstrapVue in your vue.js project step by step.

How to install or configure vue-bootstrap in VueJS?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. the component name is FirstComponent.vue and imported into the App.js file:

App.js

<template>
  <div id="app">
    <FirstComponent />
  </div>
</template>

<script>
import FirstComponent from "./components/FirstComponent.vue";
export default {
  name: "App",
  components: {
    FirstComponent,
  },
};
</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>

Install the vue-typeahead-bootstrap package

To install the vue-typeahead-bootstrap package into our app. we have to use the following command:

npm i vue-typeahead-bootstrap

After the installation is complete.

We have to globally register the vue-typeahead-bootstrap in main.js

main.js

import Vue from 'vue'
import App from './App.vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(BootstrapVue)
Vue.use(IconsPlugin)

// Global registration
Vue.component('vue-typeahead-bootstrap', VueTypeaheadBootstrap)

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

Add autocomplete component to your app

Let’s use the VueTypeaheadBootstrap in our app. And also we are going to call the event after the value is selected from the autocomplete. Let’s see the code example:

FirstComponent.js

<template>
    <b-container fluid>
        <b-row>
            <b-col md="6" offset-md="3">
                <h1>Welcome to sortoutcode.com</h1>
                <div class="pl-1 pb-2 pt-3">Selected Country: {{ selected }}</div>
                
                <div>
                    Options: {{optionData}},
                    <vue-typeahead-bootstrap 
                        :data="optionData" 
                        v-model="query"
                        @hit="optionSelect"
                        placeholder="Choose a country"
                    />
                </div>
            </b-col>
        </b-row>
    </b-container>
</template>

<script>

export default {
    name: "FirstComponent",

    data() {
        return {
            query: "",
            optionData:['Nepal','Africa','Chine','Canada', 'US', 'Mexico', 'India'],
            selected:'',
        }
    },
    methods:{
        optionSelect(country){
            this.selected = country;
        }
    }
};
</script>

For now, let’s check the output.

bootstrap vue autocomplete select event

All the best 👍.

Follow me on Twitter