E
E
Enickckey2022-02-21 15:30:24
Vue.js
Enickckey, 2022-02-21 15:30:24

How to dynamically load components?

Hello.
How to dynamically load components?
I get an array in props. These components all lie in the same folder at the same level. Is it possible to import them in a loop?
components:['Button','Checkbox']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
InfernoElegy, 2022-02-24
@InfernoElegy

You can simply store the components in an array rather than in the top-level components object, i.e.:

import SomeComponent from 'path/to/SomeComponent'

data: () => ({
  items: [
    { component: SomeComponent }
  ]
})

And in the template
<component :is="item.component" />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question