1

Let us assume we have 100 components. We usually add the component selector/name in HTML tags to the template of the parent component.But here we have 100's of components, so is there any dynamic way to add them??

5
  • 1
    I don't understand what you're asking, could you give an example of what you are currently doing manually? Commented May 22, 2018 at 10:54
  • link In that example pie, bar are the components. We add them using <pie></pie> and <bar></bar> tags respectively in main component.But what if we have 100 components like that. Is there any dynamic way to add them. Commented May 22, 2018 at 11:13
  • I still don't get it, there's no pie or bar in the example you posted. Commented May 22, 2018 at 11:20
  • link In that example pie, bar are the components. We add them using <pie></pie> and <bar></bar> tags respectively in main component.But what if we have 100 components like that. Is there any dynamic way to add them **I'm so sorry I forgot to save it ** Commented May 22, 2018 at 11:36
  • So you have 100 different components? Max Sinev posted an answer that should work in that case, but you still need to write a list of all the components obviously. It might be possible that there's a better way than having those 100 components in the first place, but hard to tell with the given info. Commented May 22, 2018 at 11:47

4 Answers 4

3

There is a Vue tag:

<component :is="myComponent"></component>

Where myComponent is component name or whole component object. You can create array with component names and render them with v-for dynamically. See Vue Docs for details about dynamic component.

Sign up to request clarification or add additional context in comments.

6 Comments

This is a nice solution. did not know it existed.
<component v-for="comp in components" :key="comp" :is="comp"></component> components=['pie','bar'] This didn't work
Do you register components somehow with Vue.component or components section in parent component?
Yes I have added them. <pie></pie> <bar></bar> this worked well
Before use them in markup you should register components with one of provided ways in code. Here jsfiddle with example how to achieve that: link
|
0

Yes there is a way to dynamically add components.

In general there is three things. 1. instantiate the component 2. mount the component 3. add it to the dom tree

var ComponentClass = Vue.extend(Component)
var instance = new ComponentClass() //instantiate
instance.$mount()  //mount
this.$refs.container.appendChild(instance.$el) //add to dom

1 Comment

Ah, I might have misunderstood the question and answered if it is possible to programmatically add components.
0

Please don't do this. The <component :is> trick is handy to know, but this is a bit like getting a wedding invitation with the name of the bride on a sticky label - not convincing. You need to commit yourself sometime. The template with lots of of <component :is> tags will be impossible to understand and maintain.

1 Comment

I extremely agree with you, component is useful in just few situations. But the solution is to apply just single component tag with v-for directive that will improve code understanding instead of adding 100s of similar components in markup(causes increasing app bundle size as well).
0

This worked!!!!!

<div v-for="comp in components" :key="comp">
    <component :is="comp"></component>
 </div>

1 Comment

It doesn't need div wrapper in general, please see my jsfiddle link in my answer comment. I think you can mark my answer as correct one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.