Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  122] [ 6]  / answers: 1 / hits: 43802  / 6 Years ago, thu, october 4, 2018, 12:00:00

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.


More From » vuejs2

 Answers
30

You can recursivly go through your files and import them by their component name or file name.



 const files = require.context('./', true, /.vue$/i)

files.keys().map(key => {
Vue.component(files(key).default.name ?? key.split('/').pop().split('.')[0], files(key).default);
})


[#53374] Saturday, September 29, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bobbyw

Total Points: 456
Total Questions: 102
Total Answers: 113

Location: Bahrain
Member since Fri, Sep 16, 2022
2 Years ago
;