Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJs: Pass Dynamic Components as props to another Component and render them

I am trying to build a table component.

I want to define and pass the column metadata for the grid as an array prop and also pass the actual data as another prop to the grid.

I was able to achieve that without many issues.

But, now I would like to pass a dynamic component as a part of each column definition so that the user can define/control the way the cell gets rendered (content with edit delete buttons in same cell etc.)

Is there a way to pass a dynamic component as a prop and then have this component rendered?

<parent-comp>
  <tr class="" v-for="result in dataSource">
    <template v-for="column in columns">
      <td>
        <template v-if="column.customComponent">
          ######## How do I render this customComponent ########
        </template>
      </td>
    </template>
  </tr>
</parent-comp>

where the dataSource data can be something like

[
  columns: [{
    name: "something",
    customComponent: SomeCustomComponent
  }, {
    name: "another thing",
    customComponent: AnotherOtherCustomComponent
  }]
]

Will be happy to elaborate/clarify on this if the ask above is not clear.

like image 387
Chandu Avatar asked Jul 14 '17 15:07

Chandu


People also ask

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.

Can we pass component as a prop in Vue?

Data in props can only flow one way – from the top, or parent component, to the bottom, or child components. This simply means you cannot pass data from a child to a parent.

How do I pass Props from parent to child in Vue?

The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.


1 Answers

As suggested in the comments above, you can use a dynamic component in your template and pass the definition of the component in your property.

console.clear()

const ColumnOne = {
  template: `<h1>I am ColumnOne</h1>`
}

const ColumnTwo = {
  template: `<h1>I am ColumnTwo</h1>`
}

Vue.component("parent-comp",{
  props:["columns"],
  template:`
    <div>
      <component v-for="column in columns" 
                 :is="column.customComponent" 
                 :key="column">
      </component>
    </div>
  `
})

new Vue({
  el:"#app",
  data:{
    columns:[{
      name: "something",
      customComponent: ColumnOne
    }, {
      name: "another thing",
      customComponent: ColumnTwo
    }]
  }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="app">
  <parent-comp :columns="columns"></parent-comp>
</div>
like image 133
Bert Avatar answered Sep 28 '22 03:09

Bert