Vue3 可组合渲染

BT1*_*101 3 vue.js vue-render-function vuejs3 composable

是否可以创建一个使用渲染函数的可组合函数,以便它可以显示某些内容?

例子:

import { h } from 'vue'

export function useErrorHandling() {
  return {
    render() {
        return h('div', { class: 'bar', innerHTML: 'world!' })      
    }
  }
}
Run Code Online (Sandbox Code Playgroud)
<script setup>
import { useErrorHandling } from './mouse.js'

 useErrorHandling()
</script>

<template>
 hello
</template>
Run Code Online (Sandbox Code Playgroud)

与上面的例子的plaground

小智 5

是的,您只需将可组合项返回的值存储在变量中并将其用作组件即可

const err =  useErrorHandling()
//in template 
// <err />
Run Code Online (Sandbox Code Playgroud)

游乐场示例