Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering vue components globally

I have a vue app that I created using the vue-cli

Im creating some components and I want to use them like this:

<template>     <oi-list>         <oi-list-header>Task ID</oi-list-header>         <oi-list-header>Tasks Title</oi-list-header>          <oi-list-header>Task Status</oi-list-header>          <div v-for="task in tasks">             <oi-list-item>{{ task.id }}</oi-list-item>             <oi-list-item>{{ task.title }}</oi-list-item>             <oi-list-item>{{ task.status }}</oi-list-item>         </div>     </oi-list> </tempalte> 

The probelm I have is where ever I use the list component I have to write the following:

<script>     import List from '@/components/List'     import ListHeader from '@/components/ListHeader'     import ListItem from '@/components/ListItem'      export default {     name: "Tasks",     components: {         'oi-list': List,         'oi-list-header': ListHeader,         'oi-list-item': ListItem     } <script> 

What I would like is for reusable components to either be registered globally so i dont have to import them and their sub components every time i want to use them, or some how have them load dynamically when I use them. Is this possible?

I have used Vuetify in the past and that doesn't require you to import each component in order to use it.

Please can someone point me in the right direction? Thanks.

like image 324
Brad Avatar asked Oct 04 '18 10:10

Brad


People also ask

How do I register a component in VueJS?

Using a Component To expose the imported component to our template, we need to register it with the components option. The component will then be available as a tag using the key it is registered under. With <script setup> , imported components are automatically made available to the template.

How do I import Vue components?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.

How do I register a component in Vue 3?

Here's a quick example of what that might look like. import Vue from "vue";import PopupWindow from "./components/PopupWindow";import App from "./App. vue";Vue. component("PopupWindow", PopupWindow); // global registration - can be used anywherenew Vue({ render: (h) => h(App),}).


2 Answers

This is easy to accomplish. You can register components globally in the main.js file.

import MyComponent from '@/components/MyComponent'  Vue.component('my-component-name', MyComponent) 

Now you can use <my-component-name /> everywhere.

A cleaner way, without bloating your main.js, is to import your component in an index.js file in your components folder like this.

import Vue from 'vue' import MyComponent from '@/components/MyComponent'  Vue.component('my-component-name', MyComponent) 

Later you can add this line to your main.js to load the registered components.

import '@/components' 

the docs: https://vuejs.org/v2/guide/components-registration.html

like image 145
Odyssee Avatar answered Oct 01 '22 12:10

Odyssee


As another option in addition to @Odyssee's answer, if you want to avoid globals, is to create a file, say globalComponents.js with the following contents:

import List from '@/components/List.vue' import ListHeader from '@/components/ListHeader.vue' import ListItem from '@/components/ListItem.vue' export default {     'oi-list': List,     'oi-list-header': ListHeader,     'oi-list-item': ListItem } 

And you can use it as follows:

<script>     import GlobalComponents from '@/globalComponents.js'      export default {     name: "Tasks",     components: {         ...GlobalComponents     } <script> 
like image 32
Ayush Gupta Avatar answered Oct 01 '22 11:10

Ayush Gupta