I used to use React.js but currently I'm learning Vue.js.
Now I need to dynamically render a part of an element, so I tried to render an jsx like this:
This is a simplified example:
UserListView.vue
<template>
<UserList :users="users" :columns="columns">
</template>
<script>
import UserList from './UserList';
export default {
components: {
UserList,
},
data() {
return {
users: [
{ id: 0, name: 'Alice', age: 18, gender: 'F' },
{ id: 1, name: 'Bob', age: 18, gender: 'M' },
],
columns: [
{ name: 'name', label: 'Name' },
{ name: 'age', label: 'Age' },
// element with custom render function
{ name: 'gender', label: 'Gender', render: user => (
<div>{ user.gender === 'M' ? 'Male' : 'Female' }</div>
) },
]
}
}
};
</script>
UserList.vue
<template>
<table class="UserList">
<thead>
<tr>
<th v-for="column in columns" :key="`column_${column.name}`" :class="column.name">
{{ column.label }}
</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="`user_${user.id}`">
<td v-for="column in columns" :key="column.name" :class="column.name">
{{column.render ? column.render(user) : user[column.name]}}
</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
props: ['users', 'columns'],
};
</script>
But this throws me an error:
So is there a way that I can pass a template into the child and tell it how to render with some arguments? Can it be done with slot? Or is there another way to do it?

UserListto render each cell what I like. Its render behavior is determined by the parent instead of chaining every possibility inUserListitself.<slot>for, but I have no clue how to use<slot>for your particular use case.<div>${ user.gender === 'M' ? 'Male' : 'Female' }</div>? vue will render it as string.