Skip to content

Vue模板中渲染VNode

参考stackoverflow文章

vue2

vue

<template>
  <hello name="hello"></hello>
</template>
<script>
  export default {
    components: {
      hello: {
        props: {
          name: String
        },
        render(h) {
          return h('span', this.name)
        }
      }
    },
  }
</script>

vue3

单个VNode

vue
<template>
  <hello></hello>
</template>
<script setup>
  import {h} from 'vue'
  const hello = h('span', 'hello')
</script>

多个VNode

vue
<template>
  <component :is="hello"/>
</template>
<script setup>
  import {h} from 'vue'
  const hello = [
    h('span', 'hello'),
    h('span', 'hello1'),
    h('span', 'hello2')
  ]
</script>
/src/technology/dateblog/2025/06/20250618-vue%E6%A8%A1%E6%9D%BF%E4%B8%AD%E6%B8%B2%E6%9F%93vnode.html