I have a Vuejs component that looks like this:
<template>
<button @click="generateItem()">Add item</button>
<div class="container"></div>
</template>
<script setup>
import { h } from 'vue'
import MyItem from '@/components/MyItem.vue'
function generateItem(){
return h(MyItem)
}
</script>
I’m trying to use an h()
function to render an instance of MyItem
inside div.container
when running generateItem()
. But after reading the official Vuejs docs on the h()
function I don’t get how to make this happen. Thoughts on how to get this done?
2
Answers
I believe the render function is not meant to be used with
<script setup>
.It is used as a flexible alternative to declare your render function programmatically, instead of using a
template
within a Vue SFC.That said, you can achieve rendering a generated item like so:
(see this playground url here: Vue SFC Playground)
This also works if you wrap with
h()
, but I believe it to be unnecessary:Use
v-for
(adapt to your needs):Playground