onMounted
lifecycle hook in the setup function.
This example uses an Algolia index
of ecommerce products as a source.
You could use any other source or sources you like.
Before you begin
The guide assumes that you’re familiar with the basic Autocomplete configuration options and have an existing Vue 3 app to which you want to add an Autocomplete menu.Get started
Start by adding a container for your autocomplete menu. This example adds adiv with autocomplete as an id.
Vue
algoliasearch package,
autocomplete and
getAlgoliaResults from the
autocomplete-js package.
Finally, it imports autocomplete-theme-classic package for some out of the box styling.
Depending on your desired sources,
you may need to import other packages including plugins.
Include some starter code to insert the autocomplete menu into:
Vue
Add an Algolia source
Theautocomplete-js
package provides a built-in getAlgoliaResults
function for querying an Algolia index.
It requires an Algolia search client
initialized with an Algolia application ID and API key.
It lets you search into your Algolia index using an array of queries,
which defines one or more queries to send to the index.
For more information how to use the getAlgoliaResults function,
see the Getting Started guide.
Attach the autocomplete menu to the DOM
You can instantiate and mount your Autocomplete instance in theonMounted
lifecycle hook in the setup function.
Doing so requires passing the renderer and render parameters.
This is because the default Autocomplete implementation uses Preact’s version of createElement,
Fragment and render.
Without providing Vue’s version of these,
the Autocomplete instance won’t render the views properly.
Vue
Customize templates
Next, to display the results from Algolia, you need to define anitem template.
Vue
Further UI customization
If you want to build a custom UI that differs from theautocomplete-js output,
check out the guide on creating a custom renderer.
This guide outlines how to create a custom React renderer,
but the underlying principles are the same for any other frontend framework.