DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Vue Quick Tips: Globally Registering Vue Components

Vue Quick Tips: Globally Registering Vue Components

Let's look at how to globally register components in Vue

Johnny Simpson user avatar by
Johnny Simpson
CORE ·
Feb. 28, 22 · Web Dev Zone · Tutorial
Like (3)
Save
Tweet
5.46K Views

Join the DZone community and get the full member experience.

Join For Free

When we use vue, it's common to register a component within another component. In this tutorial, we're going to look at how you can globally register a component in Vue, so you never have to reference it in your component again - instead, you can use it straight in your <template> tag. 

If you are new to Vue, check out our guide on creating your first Vue application before starting.

Registering Components in Vue

It's normal in Vue to see something like this, where a component is registered within another component, for use inside the tag:

Vue.js Component
 
<template>
    <MyComponent />
</template>
<script>
import MyComponent from '../components/MyComponent.vue';

export default {
    name: "ParentComponent",
    components: {
        MyComponent
    }
}
</script>

This is useful for components that may not be needed everywhere in the app, but it is quite normal to have a component that is used almost everywhere in your app. To save yourself referencing it in every file, you can globally register it instead.

How to Globally Register a Component in Vue

To globally register a component in vue, open your main.js file. You should see something like this:

JavaScript
 
import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

When we want to globally register a component in Vue, we need to do it in this file. All you have to do is import your component, as you usually would, and then register it using app.component.

JavaScript
 
import { createApp } from 'vue'
import App from './App.vue'

// Import your component
import MyComponent from '../components/MyComponent.vue';

// Your app
const app = createApp(App);

// Globally register your component
app.component('MyComponent', MyComponent);

// Mount your app
app.mount('#app');

Now we can reference our <MyComponent /> component from anywhere within our Vue app. app.component() has two arguments - the first is the name we are giving to our component, and the second is the imported component.

As such, we can now simplify our original code to just this:

Vue.js Component
 
<template>
    <MyComponent />
</template>
<script>
export default {
    name: "ParentComponent"
}
</script>

If you are new to Vue, check out our guide on creating your first Vue application before starting.

app application Vue.js JavaScript IT

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What I Miss in Java, the Perspective of a Kotlin Developer
  • No-Code/Low-Code Use Cases in the Enterprise
  • Major PostgreSQL Features You Should Know About
  • Monolith vs Microservices Architecture: To Split or Not to Split?

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo