Jurgen Jurgen - 5 months ago 23
Javascript Question

How to bind dynamic props to dynamic components in VueJS 2

I'd like to know how I can iterate a list of component names (which come from an AJAX call to an API server) and render them as components, and pass relevant properties to each component (i.e. bind their properties dynamically).

So far I have managed to iterate a JSON list of items that represent components, and successfully render these components. What I'd like to do now is bind the properties for each component using


In the example below, the
component would receive the
property with the
value; and the
component wouldn't receive any properties.

<div v-for="item in items">
<component :is="Object.keys(item)[0]" :v-bind="???"></component>

import ItemOne from '../components/item-one'
import ItemTwo from '../components/item-two'

export default {
components: {
asyncData () {
return {
items: [
{ 'item-one': { 'image': 'item1.jpg' } },
{ 'item-two': { } }

I tried using
but I get the attribute
v-bind="[object Object]"
in the rendered element.

Answer Source

First, you need to get rid of the colon before v-bind. The colon is a short-hand for v-bind when prefixed to an attribute. But, when passing an object of key pairs to bind, you simply use the v-bind directive.

Second, you are not correctly referencing the properties of each item. You can reference them like this:


You wouldn't need to use Object.keys if you changed the structure of your items property a bit:

items: [{ 
  type: 'item-one', 
  props: { 'image': 'item1.jpg' },
}, {
  type: 'item-two',

This way, by explicitly labeling the component type and properties beforehand, your template would be much easier to understand:

<div v-for="item in items">
  <component :is="item.type" v-bind="item.props"></component>