Overriding components
Overview
All components existing in the Vue SDK are exposed and can be extended or overridden. This allows the logic and structure to be adjusted to needs of the widgets and to customize the overall behavior. Compared to extending templates, this is the more advanced approach to modify the component that includes not only layout, but also functionality and structure modifications.
Â
Prerequisite
Â
Steps to extend components
Create a components folder in the projects vue directory on the same level as the widgets folder
Create a component file with .vue extension (e.g. ResultItem.vue) and open for edit. This file should follow the structure of Vue.js single file components
In the <script> section import the same component
import { ResultItem } from '@hawksearch/vue';
Â
Make the required adjustments. For example creating a computed properties for some of the fields
export default { extends: ResultItem, computed: { title: function () { return this.getField('title'); }, content: function () { return this.getField('content'); } } };
Note that the package component is passed to the extends field
Â
Save the file. Now we can directly use the computed properties in the template
<div><h3>{{ title }}</h3></div> <div>{{ content }}</div>
Â
Go through the steps for creating a widget (e.g. SPA)
On top of the usual configuration, import the newly created component
Â
The ResultItem component is included in the ResultListing. At this point the used component is from the package and should be swapped with the new extended one
Import the ResultListing from the package
Â
Change the component’s reference
Â
From here, the new custom component will be rendered instead of the default. Save, re-build and preview the page.
Â