1

does anyone know of a way to dynamically render vue.js components? For example, I have a table component that should render different buttons based on the prop definition:

Vue.component('my_table', {
    template: '#my_table',
    props: {
      data: Array,
      columns: Array,
      actions: Array
    }
});

Ideally, my template would be defined in the tmpl value of my action object:

<tbody>
    <tr v-for="entry in data">
      <td v-for="key in columns">
        {{entry[key.field]}}
      </td>
      <td v-for="action in actions">
        {{ action.tmpl }}
      </td>
    </tr>
</tbody>

Here's my fiddle:

https://jsfiddle.net/zfya92dh/43/

Anyone have any ideas?

Thanks in advance!

1

2 Answers 2

1
<component :is="action.tmpl"></component>

Here is your fiddle revised.

const button1 = Vue.extend({
    template:'<button class="btn btn-primary">View</buttton>'
})

const button2 = Vue.extend({
  template:'<button class="btn btn-primary" @click="some_method">Delete</buttton>',
  methods:{
     some_method(){
         alert('clicked')
     }
  }
})

And the relevant data.

 data: {
    actions: [
    {
      name: 'view',
      label: 'View Company',
      method: 'click',
      tmpl: button1
    },
    {
      name: 'delete',
      label: 'Delete Company',
      method: 'click2',
      tmpl: button2
    },
  ],
Sign up to request clarification or add additional context in comments.

Comments

1

You just want to insert HTML instead of plain text? Change

<td v-for="action in actions">
    {{ action.tmpl }}
</td>

to

<td v-for="action in actions" v-html="action.tmpl"></td>

However, if your HTML includes Vue markup, the markup will not be respected. You will need to create actual components to represent each of your configurations and then use :is to tell Vue which one to use at a given time. Vue doesn't use string-based templating, so there is no way to make passing a template string work.

Comments

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.