Skip to content

Instantly share code, notes, and snippets.

@swthate
Last active March 29, 2019 19:51
Show Gist options
  • Save swthate/504215523c73cdb034e3ab00000755fc to your computer and use it in GitHub Desktop.
Save swthate/504215523c73cdb034e3ab00000755fc to your computer and use it in GitHub Desktop.
Vue component with array of objects as a prop
<town-form :fields="[ { component:'TownFieldTitle', name:'title' } ]"></town-form>
<template>
<div>
<!-- Dynamic list of fields from template -->
<Component v-for="(field, index) in fields" :is="field.component" :key="index" v-model="fields[field.name]" />
</div>
</template>
<script>
export default {
name: 'TownForm',
props: {
fields: {
type: Array,
default: function () {
return [ { component: 'TownFieldTitle', name: 'title' } ]
}
}
}
}
</script>
@swthate
Copy link
Author

swthate commented Mar 29, 2019

Jake Dohm is the genius behind this.

Todo:

  • Loop over fields prop and import each component.
  • How to handle fields that require a little extra effort? What if they have extra directives, or need content for slots?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment