{"id":31072,"date":"2022-10-07T11:20:46","date_gmt":"2022-10-07T11:20:46","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=31072"},"modified":"2026-02-05T09:13:32","modified_gmt":"2026-02-05T09:13:32","slug":"register-common-components-globally-in-vuejs","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/register-common-components-globally-in-vuejs","title":{"rendered":"Best Way to Register Common Components Globally in VueJS"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>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.<\/p>\n<p>In general, there are two ways to register a component<\/p>\n<p>(1) Globally<\/p>\n<p>(2) Locally<\/p>\n<p>Global registration isn&#8217;t ideal in all cases, but what if you want to share the same child components in many components? Let&#8217;s learn how to register common components globally in VueJS!<\/p>\n<h2>Prerequisites<\/h2>\n<p>Before moving forward we hope you are aware of-<\/p>\n<p>1. Basic knowledge of Vue.js<\/p>\n<p>2. Vue CLI set up<\/p>\n<h2>Tutorial Goal<\/h2>\n<p>So what will we cover in today&#8217;s tutorial on the best way to register common components globally in VueJS?<\/p>\n<ul class=\"bullets text-left\">\n<li>Register components locally<\/li>\n<li>Register components globally<\/li>\n<li>Best way to register common components<\/li>\n<\/ul>\n<h2>Initial Set Up<\/h2>\n<p>Let&#8217;s assume that we have created a project using the <a href=\"https:\/\/www.bacancytechnology.com\/blog\/plugins-to-boost-vuejs-and-vue-cli3-performance\" target=\"_blank\" rel=\"noopener\">vue cli<\/a>.<\/p>\n<p>To understand this, let&#8217;s take, for example, the most commonly created components we&#8217;ll use throughout the app. We have common base components such as BaseInput, BaseButton, BaseCard, BaseCheckbox, BaseForm, BaseSelect, etc., with basic functionalities.<\/p>\n<h2>Local Registration<\/h2>\n<p>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:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/import-and-register-components-in-the-script-section-min.png\" alt=\"import and register components in the script section\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-31079\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/import-and-register-components-in-the-script-section-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/import-and-register-components-in-the-script-section-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/import-and-register-components-in-the-script-section-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/import-and-register-components-in-the-script-section-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>With this, we have successfully registered each component locally.<\/p>\n<p>We might need to use these components repeatedly. To avoid this repetition, we can register these base\/common components globally so that we can use them in several components of our app.<\/p>\n<h2>Global Registration<\/h2>\n<p>Navigate to the file <mark>src\/main.js<\/mark>. Import the components and register them as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/Import-the-components-min.png\" alt=\"Import the components\" width=\"1100\" height=\"600\" class=\"alignnone size-full wp-image-31080\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/Import-the-components-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/Import-the-components-min-300x164.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/Import-the-components-min-1024x559.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2022\/10\/Import-the-components-min-768x419.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<h3>What&#8217;s the problem here?<\/h3>\n<p>What will happen when our app starts growing and has tons of common components? Do we need to register them one by one and use them? Yes, that can be done, but it&#8217;s an inefficient and old-school way of doing it! And whenever we create a new common component, we must go back and forth between the new component and the src\/main.js file to register and use them.<\/p>\n<h3>What is the better way?<\/h3>\n<p>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&#8217;s see its implementation.<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Create Global Component Container<\/span><br \/>\nWith global registration and using the app.component() method: make components available globally in the current Vue application. <a href=\"https:\/\/www.bacancytechnology.com\/hire-vuejs-developer\" target=\"_blank\" rel=\"noopener\">Hire Vue developer<\/a> to get the job done<\/i><\/strong><\/p>\n<h2>Best Way to Register Common Components Globally in VueJS<\/h2>\n<p>Install the Lodash library using the below command.<\/p>\n<pre>npm install lodash --save<\/pre>\n<p>Navigate to <mark>src\/main.js<\/mark> and add the following code to the file.<\/p>\n<p><strong>Import Lodash&#8217;s upperFirst and camelCase functions<\/strong><\/p>\n<pre>import Vue from \"vue\";\r\nimport App from \".\/App.vue\";\r\nimport router from \".\/router\";\r\nimport store from \".\/store\";\r\nimport upperFirst from 'lodash\/upperFirst'\r\nimport camelCase from 'lodash\/camelCase'<\/pre>\n<p><strong>Use require. context to search every component in the src\/components\/base directory that starts with Base.<\/strong><\/p>\n<pre>const requireComponent = require.context(\r\n  '~\/components\/base',\r\n    true,\r\n   \/Base[A-Z]\\w+\\.(vue|js)$\/\r\n)<\/pre>\n<h3>Explanation<\/h3>\n<ul class=\"bullets text-left\">\n<li>Here, the first argument for require context is the relative path of the components folder, which in our case is &#8216;~\/components\/base&#8217;.<\/li>\n<li>The second argument determines whether to look for subfolders. You can set this parameter to true\/false based on the components hierarchy.<\/li>\n<li>The third argument we have to pass is the regex to match component filenames, which in our case is Base.<\/li>\n<\/ul>\n<p><strong>We&#8217;re going to convert component names to the pascalCase format.<\/strong><\/p>\n<pre>requireComponent.keys().forEach(fileName => {\r\n  const componentConfig = requireComponent(fileName)\r\n\r\n  const componentName = upperFirst(\r\n    camelCase(\r\n      fileName\r\n        .split('\/')\r\n        .pop()\r\n        .replace(\/\\.\\w+$\/, '')\r\n    )\r\n  )\r\n<\/pre>\n<p><strong>Now that we have all the components&#8217; names, register them globally.<\/strong><\/p>\n<pre>Vue.component(\r\n componentName,componentConfig.default || componentConfig\r\n)<\/pre>\n<p><i>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.<\/i><\/p>\n<h2>Github Repository<\/h2>\n<p>Visit the source code here and feel free to clone the repository. You can try it your way and explore more about it.<\/p>\n<p><strong>Github link:<\/strong> <a href=\"https:\/\/github.com\/nency-gajjar\/best-way-to-register-common-component\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/nency-gajjar\/best-way-to-register-common-component<\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>And that&#8217;s pretty much it! This is how we can register common components globally in VueJS 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 VueJS concepts, visit the <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/vuejs\" target=\"_blank\" rel=\"noopener\">VueJs tutorials page<\/a>. Every blog post has a github source code repository; clone that repo, and start coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t ideal in all cases, but [&hellip;]<\/p>\n","protected":false},"author":129,"featured_media":31083,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1244],"tags":[],"coauthors":[2096],"class_list":["post-31072","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vuejs"],"acf":[],"modified_by":"Aditya Goswami","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/31072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/129"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=31072"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/31072\/revisions"}],"predecessor-version":[{"id":57593,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/31072\/revisions\/57593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/31083"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=31072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=31072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=31072"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=31072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}