ITS
Vue.js development
Introduction
Components in Vue are like widgets. We can write reusable custom elements the way we want. Components are nothing but objects consisting of all the options contained by root or Vue instance.
In general, there are two ways to register a component
(1) Globally
(2) Locally
Global registration isn’t ideal in all cases, but what if you want to share the same child components in many components? Let’s learn how to register common components globally in Vue.js!
Before moving forward we hope you are aware of-
1. Basic knowledge of Vue.js
2. Vue CLI set up
Let’s assume that we have created a project using the vue cli.
To understand this, let’s take, for example, the most commonly created components we’ll use throughout the app. We have common base components such as BaseInput, BaseButton, BaseCard, BaseCheckbox, BaseForm, BaseSelect, etc., with basic functionalities.
In order to use these base components, we have to import and register components in the script section of each parent component as shown below:
Well, we can register each base component with a few lines of code. For example, BaseButton.vue, BaseInput.vue, BaseCard.vue, and in general, every component that has the pattern Base*.vue (in this case). Let’s see its implementation.
Navigate to src/main.js and add the following code to the file.
Import Lodash’s upperFirst and camelCase functions
id=”content_7_4″ class=”lang-js s-code-block”>import Vue from “vue”;
import App from “./App.vue”;
import router from “./router”;
import store from “./store”;
import upperFirst from ‘lodash/upperFirst’
import camelCase from ‘lodash/camelCase’
Use require. context to search every component in the src/components/base directory that starts with Base.
const requireComponent = require.context(
‘~/components/base’,
true,
/Base[A-Z]\w+\.(vue|js)$/
)
We’re going to convert component names to the pascalCase format.
requireComponent.keys().forEach(fileName => {
const componentConfig = requireComponent(fileName)
const componentName = upperFirst(
camelCase(
fileName
.split(‘/’)
.pop()
.replace(/\.\w+$/, ”)
)
)
Now that we have all the components’ names, register them globally.
Vue.component(
componentName,componentConfig.default || componentConfig
)
Note: For this to work, one must follow the naming conventions while creating components. Without a regular pattern in the names of the components, this trick might not work.
Visit the source code here and feel free to clone the repository. You can try it your way and explore more about it.
Github link: https://github.com/nency-gajjar/best-way-to-register-common-component
And that’s pretty much it! This is how we can register common components globally in vue.js without importing all the components. It can be done within a few lines of code without having to worry about registering the newly created standard components again and again. We hope the step-by-step guideline was useful to you! For more such guidelines on basic and advanced vue.js concepts, visit the vue.js tutorials page. Every blog post has a github source code repository; clone that repo, and start coding.