Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add dynamic components/partials in Vue.js

I need to add child components dynamically to a component based on user interaction. I looked to some old issue but it seems like a dirty hack to me, besides, it is an old issue, and vue development is really active, so I don't know if there is some better approach now.

What I've tried so far is here on this fiddle but is obviously not working, it says that $mount can only be called once. I don't know how to do it, my other option would be dynamic component, but for that I would also have to add a <component> element dynamically so it turns to be almost the same issue.

So how could I add a component on client click or other client event?

like image 403
Yerko Palma Avatar asked Mar 10 '16 21:03

Yerko Palma


People also ask

How do I use dynamic components in Vue?

To make the component dynamic, we can bind it to a set property with the v-bind directive. Your component is now bound with the component property in the data. If you switch the component to Test2 , it will automatically mount the Test 2 component. Test it out on your browser.

How do you pass dynamic components to props?

We can pass props to dynamic components using the v-bind directive. To display dynamic components, we add the component component with the is prop. currentComponent is a string with the component name. v-bind has an object with the props that we want to pass to whatever component is being rendered now.

How do I add a class dynamically on Vue?

Adding a dynamic class name is as simple as adding the prop :class="classname" to your component. Whatever classname evaluates to will be the class name that is added to your component. Join 11,067 other Vue devs and get exclusive tips and insights delivered straight to your inbox, every week.

How do I add components to Vue?

Open the file in your code editor. Create the component's template section by adding <template></template> to the top of the file. Create a <script></script> section below your template section. Inside the <script> tags, add a default exported object export default {} , which is your component object.


2 Answers

You want to put the custom components there right from the start, and use v-if or v-for to show, hide, add, or subtract these components. You let the data drive the DOM, not managing the DOM yourself. Fiddle example: https://jsfiddle.net/f5n5z5oo/2/

You can even make the components dynamically change what type of component they are using is:

data: {
  elements: [
    { type: 'component-a' },
    { type: 'component-b' }
  ]
}
<div v-for="element in elements" :is="element.type"></div>

If you're more specific about your use case I will try to help further!

like image 86
Jeff Avatar answered Oct 21 '22 18:10

Jeff


Hope this helps someone in future : There are numerous methods of doing this , below is one way of doing this :

1. If component is already defined , simply adding a new instance of component importantly using parent keyword to define parent of component instance as below : check codepen

var Child = Vue.extend({
    template: '<div>Hello!</div>',
});

new Vue({
  el: '#demo',
  ready: function() {
    var child = new Child({
        el: this.$el.querySelector('.child-host'),
        parent: this,
    });
  },
});
like image 36
Jimmy Obonyo Abor Avatar answered Oct 21 '22 19:10

Jimmy Obonyo Abor