BT101
BT101

Reputation: 3826

Vue3 composable with render

Is it possible to create a composable function that would use render function so it can display something?

Example:

import { h } from 'vue'

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

 useErrorHandling()
</script>

<template>
 hello
</template>

plaground with above example

Upvotes: 3

Views: 2309

Answers (2)

JackChouMine
JackChouMine

Reputation: 1200

createApp and mount can help you.

function HelloWorld({ fontSize }) {
  return h(
    'div',
    {
      style: {
        color: 'red',
        fontSize,
      },
    },
    'Hello World'
  )
}
const app2 = createApp(HelloWorld, {
  fontSize: '30px',
})
app2.mount('#app-2') // pass selector you want to mount

Upvotes: 0

Youness Ait Ali
Youness Ait Ali

Reputation: 336

Yes It is possible to do that you need just store the value returned by the composable in a variable and use it as a component

const err =  useErrorHandling()
//in template 
// <err />

Playground Example

Upvotes: 6

Related Questions