CLARIFICATION
I realize that I did not write my question very well (sorry about that). I have therefore rewritten my question and the accompanying examples.
I want to create a number of components using the Render Function in VueJS. Each function will basically be the same format, just the data inserted will change. It seems to me rather repetitive (and not at all DRY) to rewrite the render function each and every time just to create different components (when the basic structure of each component is the same).
As such, what I'd like to do is create an array that holds all the data I want to use for the various different components. I then want to loop through that data, running the Render Function each time.
For instance, imagine that this is my data:
var components = [
{ name: 'label',
props: ['tag', 'type', 'size', 'color', 'direction'],
class: 'label',
tagOption: true,
tag: 'div'},
{ name: 'icon',
props: ['type', 'size', 'color'],
class: 'icon',
tagOption: false,
tag: 'i'}
]
Running the loop on this data would be the equivalent of writing the Render Function twice as follows:
Label Component
export label {
props: ['tag', 'type', 'size', 'color', 'direction'],
render(createElement) {
let classes = ['ui', 'label']
if (this.type) { classes.push(this.type) }
if (this.size) { classes.push(this.size) }
if (this.color) { classes.push(this.color) }
if (this.direction) { classes.push(this.direction) }
return createElement(
this.tag || 'div',
{ class: classes },
this.$slots.default
);
}
}
Icon Component
export icon {
props: ['type', 'size', 'color'],
render(createElement) {
let classes = ['ui', 'label']
if (this.type) { classes.push(this.type) }
if (this.size) { classes.push(this.size) }
if (this.color) { classes.push(this.color) }
return createElement(
'i',
{ class: classes },
this.$slots.default
);
}
}
In summary
This is what I do not want to do: Have a label.js (or label.vue) file which runs the Render Function to create a label component and a separate icon.js file which runs the basically the exact same Render Function to create an icon component
This is what I do want to do: Have one file which would loop through an array of data, running and exporting that data each loop through the Render Function.
Is this possible? If so, any ideas on how to do it?
Thanks.
v-forand render a list of children for every person? Here's how the child nodes are created inside a render function: vuejs.org/v2/guide/render-function.html#createElement-Arguments and here'sv-foremulation: vuejs.org/v2/guide/render-function.html#v-if-and-v-for